Showing wrong calculated value when filter applied in the Grid - ag-grid

We defined an ag-grid that has three columns show last_year sales, current_year sales, and another column showing the percentage % difference between last_year sales & current_year sales and its group by customer name.
The issue is the column of percentage % difference shows correctly if NO filter applies to the grid, but once we filter on a specific customer, the percentage % difference value is calculated wrong for some reason.
Does anyone know why and how to fix that?
Thanks,
I expected the calculation of the percentage % diff value should be correct regarding what filter applies or not.

Related

Percentage of total changes on applying filter

I have a data table with three dimensions and one measure. For each row, I am trying to calculate the percentage of total (calculated by taking the sum of rows) using a calculated field.
As seen in the screenshot attached, For the column titles 'Dec-19' I want the values to be a percentage of current value / grand total (calculated at the bottom as 122,187)
Screenshot of DataTable:
So e.g. for the Column B value of 2000, the Dec-19 column should be (97/122,187) * 100 = 0.079.
I have achieved this by creating a calculated field with the formula: SUM (sales) / MAX ({EXCLUDE (Column B): Sum (sales}), where sales is the measure used in the datatable.
However, upon application of filter on column B, the percentage value changes. e.g. if I select the value 2000 in my filter for column B, I get the percentage as 100%. It seems as if the percentage is being calculated based on only the rows in the filter.
Since you haven't included any sample data, I created some sample data like this, hope this resemble yours.
Thereafter, I built a cross-tab view in tableau, somewhat like yours
If that is the scenario, use a calculated field, say CF like this, instead of yours
([Sales])/
{FIXED [Col1], DATETRUNC('month', [Col3]) : sum([Sales])}
Dragging it in the view
and thus, filtering won't affect your calculation

Tableau - Toggle between 2 measures by single select filter on one basic text grid

looking to have a basic text grid by month by product, and then have a single filter on only 2 of the measures. For example:
Measures Filter
------------------
[ ] Sales <-- only show this
[X] Quantity <-- and this as options
[ ] Interest (don't show in filter)
[ ] Fee ( don't show in filter)
JAN | FEB | MAR | APR | MAY | etc ...
products
ITEM1 x x x x x
ITEM2 x x x x x
ITEM3 x x x x x
So the user can basically toggle between Sales or Quantity, and whichever they click on will fill out the x by month.
I was able to get this to work for everything except only showing the 2 measures I want to filter for. It only shows all measures for me in the filter and I can't figure out how to hide them.
What I did was
Drag Date to columns
Drag Product and MeasureNames to Rows
Can someone please help me on getting this correct? Any help or guidance is appreciated!
This is not the correct use case of FILTERS. Filters are normally used to filter (read subset) the data (read rows). Say for example If your dataset has 1000 rows/records and you want some report/visual/aggregation on say 500 rows (based on some criteria of course) you have to filter the data. For selection of argument (user based) in some report/viz, etc. Parameters are used.
Your desired example output is also a perfect case of use of Parameters. You may proceed as follows-
Step-1 Create a parameter (say parameter 1) with values as SALES and QUANTITY.
Step-2 Create a calculated field (say CF1) with the following calculation
CASE [parameter 1]
WHEN 'SALES` THEN [sales]
WHEN 'Quantity' then [Quantity]
END
Note: It is assumed that your field/column names in dataset are sales and Quantity
Step-3 Build your view (Crosstab here) with
months on Columns shelf
products on rows shelf
CF1 on text in Marks card (alternatively just double click it)
Choose measure for aggregation of CF1 as desired (default will be sum)
Step-4 right click paramter 1 and click show parameter. (select desired measure for aggregation in crosstab)
VOILA your desired view is complete. Good luck.
There is another options to add to Anil's good suggestion. You can also use Measure Names as a filter. Hide all measures apart from those you want in the filter, then put Measure Names on the filter shelf.
However, this means you can't use the hidden measures from the data source elsewhere, which can be a deal breaker. Duplicating the data source is a way around that limitation.
But if you only want to show the 2 measures and are happy with single select, then choosing the measure by parameter is the better option, as Anil suggests.

Pick group totals, grand totals and normal summarization in Tableau

I am trying to calculate average of 2 columns in another column in Tableau but difficult part is grand total is not getting average instead it is the sum of 3rd calculated field.
A B Calculated field
10 5 2
6 3 2
T 16 8 4 (Here I should get 2 instead it is taking sum of column)
Here I am unable to write separate formula for row totals and grand totals, Only one formula (Calculated Field) is allowed and when I am dragging on sheet it is by default aggregating to sum.
Note: I am expert in Crystal and BO but beginner in Tableau.
Update
Code used for LoD
{FIXED [Product Category]: AVG([Sales])}
Below image is what I got after implementation I have tried with 2 columns but the result is same if I use only one column (I am trying to get the average of sales)
You are almost there - the Grand Total by default does a SUM function you just have use the Total All Using --> Average option.
Output : Level wise SUM(Profit) later averaged across columns and rows. (Show Column Grand Total & Show Row Grand Total active)
Update: Answering the question below. To get the Row-wise avg (which is Cat1-vag in this case) you could just drop the measure and change it to AVG(). Since you needed in a Calculated Field you could use a Simple FIXED LOD. You can also uncheck aggregated measures from Analysis dropdown and have no Dimension in column or row like unlike what this example shows and still get three different averages. Cheers.
{FIXED [Cat1]:AVG([Profit])}
Check out this very smart work around from Joe Mako.
https://community.tableau.com/thread/112791
create a calc field like:
IF FIRST()==0 THEN
WINDOW_AVG(SUM([Sales]),0,IIF(FIRST()==0,LAST(),0))
END
duplicate your Category field
place "Category (copy)" on the level of detail
set the compute using for the calc field pill to use "Category (copy)"
The window function in the calculated field only takes into account what's in the view, and aggregate based on those number.

Wrong Percentage of Subtotal

I'm having an issue with tables calculations in tableau. I've got sales per manager and the target. I wanna have a calculation of the targets achievement.
That's what my data looks like and the percentage I wanna get:
Unfortunately, Tableau sums up the percentage and gives me the following results:
Target columns is a calculated field. E.g.
IF [Manager Name] = "Manager 1" THEN "190000"
The % columns is a calculated field and calculated as the following:
FLOAT([Profit]) / FLOAT([Target])
I tried to play with Table Calculations but it never worked.
You are right in that a table calculation would help solve your issue. As you have the % field now, Tableau is just summing the field for each manager to get your subtotal. To get Tableau to calculate the subtotal smartly, as you would expect, you need to modify your % field formula.
Your way: Calculate Percentage for each manager and then SUM for subtotal
Fixed Way: Indicate to Tableau that you want the field to be calculated by summing all profit values first in the partition (Location) and then divide to get the percentage. In this case (330000/532000).
You can modify your % field to look something like the following:
WINDOW_SUM(FLOAT(SUM([Profit])))/WINDOW_SUM(FLOAT(SUM([Target])))
See the below thread for some more information on how the windowing functions can help with aggregate calculations
https://community.tableau.com/thread/200670

Tableau 8.2 - how to get max and min from % difference values on table?

I'm facing problem in getting the max % and min % from a table containing % difference values.
Year-----A----------B---------C---------D---------Max %----Max Type----Min %----Min Type
2012
2013---4.30%---4.42%---4.34%---4.38%----4.42%---------B-----------4.30%---------A
The table above shows the % difference in sales from previous year. Thus 2012 shows no % (because there's no 2011). I used table calculation to compute the % difference, i.e. "Percent Difference From", compute using "Table (Down)" and "Previous".
The last four columns are what I'm having trouble doing. I want to get the max % and min % and also the corresponding types. I'm not trying to add the four columns to the existing table, but to get the correct results, as my ultimate goal is to display that results on the dashboard, i.e. on my dashboard, I want to display the highest % and its corresponding type; similarly the lowest % and its corresponding type. For example: on my dashboard, I want to display:
Highest % and type: 4.42% B
Lowest % and type: 4.30% A
So, I need to have the correct formulas to get the max % and min % and their types. These are what I did:
I tried to use WINDOW_MAX and WINDOW_MIN to display the max % and min % on the table but got funky wrong results.
1) I first get the formula in calculating the % difference from the "Customize" button from "Edit Table Calculation" window of SUM([Sales]): (ZN(SUM([Sales])) - LOOKUP(ZN(SUM([Sales])), -1)) / ABS(LOOKUP(ZN(SUM([Sales])), -1))
Then I created a calculated field of the above formula. I named the calculated field "Percent-Diff".
2) I created another calculated filed (named "Max % Difference") using the formula: WINDOW_MAX([Percent-Diff]). But it shows strange results. See image below. I don't know why it gives me 2.78% and 2.91% for 2012 and 2013 respectively. It should be 0% and 4.42% for 2012 and 2013 respectively. Something is not correct.
If it is just SUM([Sales]) instead of % difference, then I get the correct result of showing the max sales using the formula WINDOW_MAX(SUM([Sales])).
3) Also I don't know how to get the corresponding type. I tried using the formula: IF [Max % Difference] = [Percent-Diff] THEN ATTR([Product Type]). But it returns:
NULL
B
I'm not sure if the formula is correct. It looks correct on the result (i.e. "B" is correct), except that it also shows a NULL value which I don't know why. I think it's because I didn't include the ELSE part in my IF formula? But why the NULL value is shown as the first value? I want the formula to return just one value, "B". So, how to only just show "B"?
I've posted twice the problem in tableau forum, but as of now, nobody has answered my problem. I believe that my formulas are incorrect. So, if anyone here can correct the formulas to get the max % and min % from % difference values and also to get the corresponding type, then it'd be very much appreciated. Thanks a million!
It's hard to tell not knowing how your database looks like (as you didn't explicitly presented it, but I can try to infer based on the clues you left on your post). But I could reproduce something like you said using the Sample - Coffee Chain Database, and it worked out well, calculated yoy sales increase by product and then window_max of that.
What you're probably missing is the partitioning. I suggest avoiding using Table or Pane to create the partitions in more complex situations (as it will work only in that specific arrangement of fields), but rather use the dimensions to partition it.
So, your [Percent-Diff] field should be compute using [Date], and your [Max % Difference] should be compute using [Product Type]. IMPORTANT, for [Max % Difference], when you go to Edit Table Calculation, you'll have to choose the Compute using for [Percent-Diff] as well (you can choose on the top of the window)
Your formula to find which type is the max (or min) is also correct (and should only respect the partitions). Nevertheless, it is very hard to have the exact output you're expecting.
What I would do is to create 2 spreadsheets (and later combine them in a dashboard).
The 1st would be what you already got (Each product [Percent-Diff]
The second one I would change your formula (3) to just [Max % Difference] = [Percent-Diff], and use it as filter (filtering only true). I would drag both Date and Product to the sheet (you choose if you want it on columns, rows, or just detail) so I can use them to partition the table. And drag [Max % Difference] to be visualized.
That way you'll only see the product that is the max, and how much is that max.
Hope it helps