Google Data Studio: Compare daily sales to 7-day average - google-data-studio

I have a data source with daily sales per product.
I want to create a field that calculates the average daily sales for the 7 last days, for each product and day (e.g. on day 10 for product A, it will give me the average sales for product A on days 3 - 9; on Day 15 for product B, I'll see the average sales of B on days 8 - 14).
Is this possible?
Example data (I have the first 3 columns. need to generate the fourth)
Date Product Sales 7-Day Average
1/11 A 983 201
2/11 A 650 983
3/11 A 328 817
4/11 A 728 654
5/11 A 246 672
6/11 A 613 587
7/11 A 575 591
8/11 A 601 589
9/11 A 462 534
10/11 A 979 508
11/11 A 148 601
12/11 A 238 518
13/11 A 53 517
14/11 A 500 437
15/11 A 684 426
16/11 A 261 438
17/11 A 69 409
18/11 A 159 279
19/11 A 964 281
20/11 A 429 384
21/11 A 731 438
1/11 B 790 471
2/11 B 265 486
3/11 B 94 487
4/11 B 66 490
5/11 B 124 477
6/11 B 555 357
7/11 B 190 375
8/11 B 232 298
9/11 B 747 218
10/11 B 557 287
11/11 B 432 353
12/11 B 526 405
13/11 B 690 463
14/11 B 350 482
15/11 B 512 505
16/11 B 273 545
17/11 B 679 477
18/11 B 164 495
19/11 B 799 456
20/11 B 749 495
21/11 B 391 504
Haven't really tried anything. Couldn't figure out how to do get started with this)

This may not be the super perfect solution but it does give your expected result in a crude way.
Cross-join the same data source first as shown in the screenshot
Use the calculated field to get the last 7 day average
(CASE WHEN Date (Table 2) BETWEEN DATETIME_SUB(Date (Table 1), INTERVAL 7 DAY) AND DATETIME_SUB(Date (Table 1), INTERVAL 1 DAY) THEN Sales (Table 2) ELSE 0 END)/7
-

Related

What is the behavior of iscntrl?

The function iscntrl is standardized. Unfortuneately on C99 we have:
The iscntrl function tests for any control character
Considering the prototype which is int iscntrl(int c); I am expecting something like true for 0..31 and perhaps 127 too. However in the following:
#include <stdio.h>
#include <ctype.h>
int main()
{
int i;
printf("The ASCII value of all control characters are ");
for (i=0; i<=1024; ++i)
{
if (iscntrl(i)!=0)
printf("%d ", i);
}
return 0;
}
I get this output:
The ASCII value of all control characters are 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 127 264 288 308 310 320 334
336 346 348 372 374 390 398 404 406 412 420 428 436 444 452 458 460 466 468 474
476 484 492 500 506 512 518 530 536 542 638 644 656 662 668 682 688 694 700 706
708 714 716 718 760 774 780 782 788 798 826 834 836 846 854 856 864 866 874 876
882 888 890 892 898 900 908 962 968 970 988 994 1000
So I am wondering how this function is implemented behind the scene. I tried to search on the standard library, but the answer is not obvious.
https://github.com/bminor/glibc/search?q=iscntrl&unscoped_q=iscntrl
Any ideas?
You are invoking undefined behavior by passing improper values to iscntrl().
Per 7.4 Character handling <ctype.h>, paragraph 1:
The header <ctype.h> declares several functions useful for classifying and mapping characters. In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined.

Predicted Values From Forecast functions

My question is very simple.
library(fpp)
ts <- ausbeer # seasonal with period 4
f.seasonal <-snaive(ts, h = 20)
I would like to see what the beer production is in the third quarter of 2010. I can do
f.seasonal$mean
It returns the table:
Qtr1 Qtr2 Qtr3 Qtr4
2008 473
2009 420 390 410 473
2010 420 390 410 473
2011 420 390 410 473
2012 420 390 410 473
2013 420 390 410
Obviously, I can see the answer in the table. Is there a snippet of code to retain the predicted value easier from forecast objects?
fc <- window(f.seasonal$mean, start=c(2010,3), end=c(2010,3))

My program returns the wrong values

I am making a program that will calculate the minimum and maximum cost of flight (supposed to be a simple program to practice for an exam) using a separate function to calculate the cost of the flight.
the code is this:
#include<stdio.h>
#include<limits.h>
float cost(float k, int ck, int n)
{
int x;
x = (k*ck)/n;
return x;
}
main()
{
int cont=1, n, nv, costmax = 0, costmin = INT_MAX, ck;
float k;
printf("Introduce the number of flights: \n");
scanf("%d", &nv);
for(cont=1; cont <= nv; cont++)
{
printf("Introduce the number of passangers on flight %d:\n", cont);
scanf("%d", &n);
printf("Introduce the number of distance on flight %d:\n", cont);
scanf("%d", &k);
if(k < 500)
{
ck=50;
}
if(k > 500)
{
ck=80;
}
cost(k,ck,n);
if(cost(k, ck, n) < costmin)
{
costmin = cost(k, ck, n);
}
if(cost(k, ck, n) > costmax)
{
costmax = cost(k, ck, n);
}
}
printf("\nMinimum cost = %d \n", costmin);
printf("\nMaximum cost = %d \n", costmax);
}
and we're supposed to use a text file to input the data
156 397 798 375 489 901 937 519 797 205 883 247 1186 738 860 967 550 887 743 753 906 582 819 665 1112 231 1009 761 921 634 686 591 1027 646 1161 424 668 413 1190 423 840 381 431 559 455 496 1105 489 848 775 456 637 664 760 412 689 639 752 669 312 940 955 706 726 579 556 655 335 902 755 665 431 1093 627 569 310 647 327 943 354 647 733 979 711 504 443 509 266 833 856 667 603 1101 670 688 898 498 669 1149 601 808 934 718 880 1053 977 556 719 1012 286 665 882 456 623 437 632 475 320 494 672 775 548 678 935 984 464 1188 641 749 816 1191 528 1092 203 770 923 1153 220 929 321 789 350 720 745 694 790 687 669 826 372 1029 392 839 932 462 806 882 539 524 797 1084 516 449 218 1048 638 751 889 448 479 465 633 1123 862 904 383 494 472 1117 365 415 889 765 670 941 341 929 876 575 940 565 967 850 473 1119 632 953 904 815 316 409 364 959 287 848 584 574 998 915 826 558 877 858 376 817 591 1068 443 447 428 1081 823 1122 373 852 598 995 735 1028 313 623 820 981 505 753 529 574 433 699 875 1032 833 1068 765 949 691 1145 358 505 251 617 417 945 694 889 323 1028 986 567 269 605 337 1153 926 590 607 803 202 1101 232 771 855 759 776 1011 878 884 393 636 230 1098 788 1140 447 1076 537 1077 734 724 266 635 232 406 752 628 743 848 537 490 598 913 416 855 640 634 209 1172 329 705 249 881 882 817
The program doesn't present any errors or warnings when compiling, but when I run it, it says that the minimum cost and the maximum cost are 0...
I've been checking everything over and over and can't find what's wrong.
Any ideas?
BTW, I'm using a linux machine to run the program, don't know if it makes a difference...
Compile with the -Wall flag, this will help you to catch errors by yourself.
Using gcc:
% gcc t.c -Wall
t.c:9:1: warning: return type defaults to ‘int’ [-Wreturn-type]
main()
^
t.c: In function ‘main’:
t.c:20:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘float *’ [-Wformat=]
scanf("%d", &k);
^
t.c:41:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
Using clang:
% clang t.c -Wall
t.c:9:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main()
^~~~
t.c:20:21: warning: format specifies type 'int *' but the argument has type 'float *' [-Wformat]
scanf("%d", &k);
~~ ^~
%f
2 warnings generated.
Clang suggests you to replace:
scanf("%d", &k);
to
scanf("%f", &k);
And even if it's not as critical, you forgot to define the return type of the main function. Both compilers have replaced it to int but you should also return something at the end of your program.
Finally, as suggested in the comments, you can also use -Wextra. I would also recommend you, while the projects are small enough and that you are still learning, to respect the "0 warning" policy. That will help you to prevent bugs.
Since k is a float, this is wrong:
scanf("%d", &k);
You need:
if (scanf("%f", &k) != 1)
break;
This uses the correct format and checks for errors. A basic debugging technique is to print out the values you've just read to ensure that the program got what you think it should have gotten.
There are other problems too. This code is redundant:
cost(k,ck,n);
if(cost(k, ck, n) < costmin)
{
costmin = cost(k, ck, n);
}
if(cost(k, ck, n) > costmax)
{
costmax = cost(k, ck, n);
}
You call the function up to 5 times to get the same answer each time. The first call you ignore altogether. You should probably use something like:
float new_cost = cost(k,ck,n);
if (new_cost < costmin)
costmin = new_cost;
if (new_cost > costmax)
costmax = cost_max;
You should also use an explicit return type for main():
int main(void)
Normally, 'passengers' is spelled with one 'a' and two 'e's.
It isn't entirely clear whether the cost() function is written appropriately. It takes one float and two int values and combines them and assigns the result to an int before returning that as a float. As written, it will work. Whether that's what you want is another matter. Since costmin and costmax are of type int, there's another level of uncertainty about what's the best type for these values.
Also, generally avoid trailing blanks in your output. A space before \n is almost always … well, if not wrong, superfluous. I'd go for almost always wrong, though. (But it is good that you end messages with a newline — that's a worse problem than trailing blanks, but prevalent in the world of C on Windows.)
Firstly, I see no reading from file. All your readings are from console (stdin).
Also, you are calling the cost function too many times, and sometimes you take no benefit from it, like here:
}
cost(k,ck,n); //<--
if(cost(k, ck, n) < costmin)
I suggest you replace indicated call with:
float c = cost(k, ck, n);
and then use c for checking/assingments instead of calling cost() all over again.
Also, you are assigning a float value to an int in multiple places:
costmax = cost(k, ck, n);
costmin = cost(k, ck, n);
In some places, you use "%d" in scanf and printf for reading/printing a float. You should use "%f".

Recursive QuickSort in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am confused why my recursive quicksort function isn't working.
Here is the original function call:
qSort(quickArr,0,inCount-1,&qSwapCount);
where inCount-1 is the position of the final array element
Here is the recursive quicksort function:
void qSort(int *qArr, int left, int right, int *qCount)
{
if(left<right)
{
int pivotIndex=(left+right)/2;
int pivot=partition(qArr, left, right, pivotIndex, qCount);
qSort(qArr, left, pivot-1, qCount);
qSort(qArr, pivot+1,right, qCount);
}
}
And here is the pivot function
int partition(int *qArr, int left, int right, int pivot, int *qCount)
{
int i;
int pivotValue=qArr[pivot];
int index=left;
swap(&qArr[pivot],&qArr[right]);
(*qCount)++;
for(i=left;i<right;i++);
{
if (qArr[i]<pivotValue)
{
swap(&qArr[i],&qArr[index]);
(*qCount)++;
index++;
}
}
swap(&qArr[index],&qArr[right]);
return index;
}
The sort must work in place, following closely to the pseudo-code provided on:
http://en.wikipedia.org/wiki/Quicksort
Thanks for your help!
Here is my output:
190 506 115 471 168 229 851 497 728 549 33 435 214 439 822 500 797 692 44 731 222 613 550 669 556 978 756 402 751 357 102 393 298 604 706 686 899 997 268 758 684 147 151 814 262 310 959 82 234 119 976 13 709 27 989 375 150 639 65 552 252 542 925 637 273 2 655 827 584 418 163 871 485 982 331 810 894 201 620 123 853 231 870 335 774 546 775 351 116 73
Obviously unsorted still :-D
You have a semicolon at the end of your for loop .. that loop body is only executed once.
for(i=left;i<right;i++);

Read file with strings and integers in matlab

I have to read a file in Matlab that looks like this:
D:\Classified\positive-videos\vid.avi 163 3 14 32 54 79 105 130 155 202 216 224 238 250 262 288 288 322 357 369 381 438 457 478 499 525 551
D:\Classified\positive-videos\vid2.avi 163 3 14 32 54 79 105 130 155 202 216 224 238 250 262 288 288 322 357 369 381 438 457 478 499 525 551
There are many such lines separated by newline. I need to read it such that: I discard path of video name and first integer(eg 163 in first line) and read rest all the numbers in an array till new line occurs. How can this be done?
You could do the following:
fid = fopen('test1.txt','r');
my_line = fgetl(fid);
while(my_line ~= -1)
my_array = regexp(my_line,' ','split');
my_line = fgetl(fid);
disp(my_array(3:end));
end
fclose(fid);
This would give you:
ans =
Columns 1 through 11
'3' '14' '32' '54' '79' '105' '130' '155' '202' '216' '224'
Columns 12 through 22
'238' '250' '262' '288' '288' '322' '357' '369' '381' '438' '457'
Columns 23 through 26
'478' '499' '525' '551'
ans =
Columns 1 through 11
'3' '14' '32' '54' '79' '105' '130' '155' '202' '216' '224'
Columns 12 through 22
'238' '250' '262' '288' '288' '322' '357' '369' '381' '438' '457'
Columns 23 through 26
'478' '499' '525' '551'
EDIT
For a numeric matrix result you can change it as:
clear;
close;
clc;
fid = fopen('test1.txt','r');
my_line = fgetl(fid);
my_array = regexp(my_line,' ','split');
my_matrix = zeros(0, numel(my_array(3:end)));
ii = 1;
while(my_line ~= -1)
my_array = regexp(my_line,' ','split');
my_line = fgetl(fid);
my_matrix = [my_matrix;zeros(1,size(my_matrix,2))];
for jj=1:numel(my_array(3:end))
my_matrix(ii,jj) = str2num(cell2mat(my_array(jj+2)));
end
ii = ii + 1;
end
fclose(fid);
This would yeild:
my_matrix =
3 14 32 54 79 105 130 155 202 216 224 238 250 262 288 288 322 357 369 381 438 457 478 499 525 551
3 14 32 54 79 105 130 155 202 216 224 238 250 262 288 288 322 357 369 381 438 457 478 499 525 551
A way easier method follows up:
fid = importdata(filename)
results = fid.data;
Ad maiora.
EDIT
Since you wanna discard the first value after the string, you will have to call
res = fid.data(:,2:end);
instead of results.

Resources