Using Tableau Prep to add 0 values - tableau-desktop

Suppose I have the data
Date Person Sales
15/01/2021 Peter 10
15/02/2021 Peter 20
15/03/2021 Peter 10
15/04/2021 Peter 30
15/05/2021 Peter 40
15/06/2021 Peter 20
15/01/2021 Sally 20
15/03/2021 Sally 10
15/05/2021 Sally 50
Sally made 0 sales in 02, 04, or 06, so no one input any sales data for her. However, when I clean the data, I would like for her to have three extra rows for when she made 0 sales so that the data says
Date Person Sales
15/01/2021 Peter 10
15/02/2021 Peter 20
15/03/2021 Peter 10
15/04/2021 Peter 30
15/05/2021 Peter 40
15/06/2021 Peter 20
15/01/2021 Sally 20
15/02/2021 Sally 0
15/03/2021 Sally 10
15/04/2021 Sally 0
15/05/2021 Sally 50
15/06/2021 Sally 0
Does anyone know how to accomplish this quickly in Tableau prep?

The easiest way for doing that is to identify your time granularity (say month) and create a flat file having all the dates you need:
15/01/2021
15/02/2021
...
15/12/2021
Then from you original datasorurce you may want to create a step with Tableau prep in order to have the distinct users:
Peter
Sally
Then you need to join those two temp tables in order to have the cartesian product:
15/01/2021 Peter
15/02/2021 Peter
...
15/12/2021 Peter
15/01/2021 Sally
15/02/2021 Sally
...
15/12/2021 Sally
Once you have that table you just need to add a column for sales (or the other metrics you need to use) with a zero/null values according to your need(probably null works better when it comes to avg and not just sum):
15/01/2021 Peter null
15/02/2021 Peter null
...
15/12/2021 Peter null
15/01/2021 Sally null
15/02/2021 Sally null
...
15/12/2021 Sally null
With this final temp table you just need to create a union using your original datasource and that final table, and you will have all your original data plus a backup just in case some user may miss any monthly row.

Related

Create calculated field with condition from another column

I have data that shows student name and the years they attended the school along with the grades they achieved each year.
I need to visualize this in a horizontal bar graph with names on the y-axis and grades on the x-axis
My dataset looks somewhat like this:
name
Year
Grade
John
2016
79
2017
65
Smith
2018
87
2019
56
Mary
2017
92
Jack
2016
95
2017
75
I want a dropdown/parameter based on the year that changes my data in a way that shows, The names and grades of the selected year only.
So if I were to select 2017, I want the data to look like this:
Name
Grade
John
65
Mary
92
Jack
75
I've tried something like this with no luck in the 'create calculated field' dialog box:
If [Parameters].[By Year] == "2017"
THEN
[Name] = WHEN [year] = "2017"
END
your calculated field (for filter) should be like this:
IF [Year] = [Parameters].[By Year]
THEN [Grade]
ELSE NULL
END

How to multiply and merge two tables in SAS using a bridge table?

I am trying to merge two SAS tables based on a third “bridge table” and perform some calculations during the process. The code should be like “For each good lookup the price and calculate the annual revenue.”
My raw data: one table with annual quantity of goods, one table with prices and one bridge table with the information which price is used for which good.
data work.goods;
input date date. GoodA GoodB GoodC;
format date year. ;
datalines;
01Jan20 10 12 2
01Jan21 12 11 5
run;`
data work.price;
input date date. PriceA PriceB;
format date year.;
datalines;
01Jan20 220 110
01Jan21 250 120
run;
data work.bridgetable;
input goods $5. price $7.;
datalines;
GoodA PriceA
GoodB PriceB
GoodC PriceB
run;
So far, I used a proc sql statement without the information in the bridge table.
proc sql;
create table work.result as
select goods.date,
goods.GoodA * price.PriceA as RevenueA,
goods.GoodB * price.PriceB as RevenueB,
goods.GoodC * price.PriceB as RevenueC
from work.goods as goods, work.price as price
where goods.date = price.date;
quit;
Now, I would like to use the information from the bridge table, so that I can change the assignment of a price to a good (e.g. instead of PriceB PriceA is used for GoodC). In addition, I’d like to have the code more dynamic without the hardcoding so that I can add new goods and prices in my tables without re-coding the ‘select’ part of my sql statement.
How do I implement the bridge table in proc sql?
Thanks a lot for your help!
Your first two tables need to be vertical and not horizontal. Then the structure will not change when new goods or new price categories are added.
You can use PROC TRANSPOSE to convert your current tables.
data goods;
input year GoodA GoodB GoodC;
datalines;
2020 10 12 2
2021 12 11 5
;`
data price;
input year PriceA PriceB;
datalines;
2020 220 110
2021 250 120
;
data bridgetable;
input goods $5. price $7.;
datalines;
GoodA PriceA
GoodB PriceB
GoodC PriceB
;
proc transpose data=goods
name=goods
out=goods_tall(rename=(col1=amount))
;
by year;
var good: ;
run;
proc transpose data=price
name=price
out=price_tall(rename=(col1=unit_price))
;
by year;
var price: ;
run;
Now the tables are easy to join.
proc sql ;
create table want as
select *,unit_price*amount as revenue
from goods_tall
natural join price_tall
natural join bridgetable
;
quit;
Results
unit_
Obs goods price year amount price revenue
1 GoodA PriceA 2020 10 220 2200
2 GoodB PriceB 2020 12 110 1320
3 GoodC PriceB 2020 2 110 220
4 GoodA PriceA 2021 12 250 3000
5 GoodB PriceB 2021 11 120 1320
6 GoodC PriceB 2021 5 120 600

Line graphs in Tableau

I have a line graph in Tableau over the past 6 months and it is filtered by person. Some people have no data for certain months. When they do not have data, the graph appears blank for that month. I would like for the graph to show 0 instead of a blank. For example, if person A had no sales in February, I would like for the graph to show 0 for February rather than a blank for February. I cannot use the Zn function because there are no null values.
Any ideas?
Thanks!
Edit: Here is an example using false data
The second graph is the same as the first graph except it is filtered by person. Even though Sally has no data in Feb, March, or June, this actually means that the value should be 0. I would like for the line graph to drop to 0 for these months. Further, then the average should be around 58 rather than 116.
Assuming you have a data like the following:
Date Person Sales
15/01/2021 Peter 10
15/02/2021 Peter 20
15/03/2021 Peter 10
15/04/2021 Peter 30
15/05/2021 Peter 40
15/06/2021 Peter 20
15/01/2021 Sally 20
15/03/2021 Sally 10
15/05/2021 Sally 50
Drag date into your colum shelf and make it Month-discrete
Drag Sales into the worksheet
Switch to line chart
Right click Sales --> Format --> Pane --> Marks --> Show at default value
Right click Month (Date) --> Show missing values
Add Person as a Filter --> show Filter
Then you should be able to get something like this having Sally (just 3 out of 5 total months) showing her non existing 2 values as zero.

VLOOKUP in a FILTER-ed range while automatically adding new rows

I have an easy-to-append monthly purchase log:
month prod count
-----------------
jan water 10
jan bread 20
feb bread 2
feb water 1
And I want to get a friendlier summary table:
prod jan feb
-------------
water 10 1
bread 20 2
Any idea how I can get this raport with new months in log appearing automatically as new columns?
I managed to get the month heads with a =ArrayFormula(TRANSPOSE(UNIQUE(FILTER(log!A2:A, log!A2:A<>"")))) and I am ok with entering the prod column by hand but I only managed to have a formula per column for count. And that means I need to drag the formula with each new month added to the log...
Any ideas? Thanks!
Try this formula:
=QUERY(A:C,"select B, sum(C) where A <> '' group by B pivot A")
See more info here:
https://developers.google.com/chart/interactive/docs/querylanguage
Use number of months instead of names to get 1, 2, 3 from feb, jan ordered alphabetically

Conditional array formulas

I have a massive dataset and am preparing a dashboard based on this dataset.
On my dashboard, I have a drop-down menu that allows me to select a month of my choice, from Jan to Apr.
Visitor Jan Feb Mar Apr
Jenny 2 3 0 1
Peter 2 0 1 3
Charley 0 2 4
Charley 1 2 2 3
Sam 1 4 2 3
Peter 2 2 5 0
John 3 3 6 9
Robin 4 0 7 0
I am looking for a formula that will give me the number of unique visitors who have been active at least once in the month that I choose from the drop-down menu.
Hoping this is really clear, but if not, please feel free to shoot back your questions.
This may be easier with Excel 2013, but if the results you want from your example are 6, 5, 5, and 5 for Jan>April respectively then perhaps:
Create a PivotTable from multiple consolidation ranges (example how here and for VALUES choose Sum of Value.
Count the non-zero values in the PT by column with a formula such as:
=COUNTIF(H5:H10,">"&0)
The above however would not be convenient for repetition each month, though a whole year might be prepared at one time.

Resources