PowerBI DAX: logic to use aggregated table as parameter in functions or another workaround to calculate dataset KPI filtered by any field? - excel

In PowerBI, I need to create a Performance Indicator (KPI) measure which evaluates dataset values in a scale from 0 to 1, with target (1) being the MAX value in a 20 years history. It's a national airport trip records open database. The formula is basically [value]/[max value].
My dataset has a lot of fields and I wish I could filter it by any of these fields, with a line chart showing the 0-1 indicator for each month based on the filters.
This is my workaround test solution:
Table 1 - Original dataset: if I filter something here, below tables also update (there are more fields to the left, including YEAR and MONTH
Table 2 - Reference to original dataset, aggregating YEAR-MONTH by the sum of "take-offs" (decolagens)
Table 3 - Reference to above (sum) table, aggregating MONTH by the max of "take-offs" (decolagens)
Table 4 - 'Sum table' merged to 'Max table' by MONTH as new table: then do [Value]/[Max] and we've got the indicator
So if i filter the original dataset by any fields, all other tables update accordingly and the indicators always stays between 0-1, works like a charm.
TL;DR
The problem is: I need to create a dashboard of this on Power Bi. So I need this calculation to be in a measure or another workaround.
My possible solution: by pure DAX code in the measure field, to produce Tables 2 and 3 so I'll divide the month sum values by their month max value (which will both be produced according to PowerBi dashboard slicers) and get the indicator dinamically produced.
I'm stuck at: I don't understand how can I reference a sum/max aggregate table in dax code. Something like = SUM (dataset[take-offs]) / MAX (SUM (dataset[take-offs])). Of course these functions do not work like that, but I hope I made my point clear: how can I produce this four table effect with a single measure?
Other solutions are welcome.
Link to the original dataset: https://www.anac.gov.br/assuntos/dados-e-estatisticas/dados-estatisticos/arquivos/DadosEstatsticos.csv
It's an open dataset, so I guess there's no problem sharing it. Please help! :)
EDIT: please download the dataset and try to solve this. Personally I think it's a quality statistics doubt that will eventually help others. The calculation works, it only needs a Power Bi Measure port.

Add the ALL formula:
Measure = SUMX(ALL('Table'),[Valor])/SUM('Table'[Max])
Example

Related

How To Convert The Grand Total of a Pivot Table as a Value for Each Row in a New Column

Overall goal for my report:
I am creating a pivot table in excel right now (eventually in Power Bi) that will update daily through data imports to reflect weekly changes in sales. I am then trying to perform a Z score analysis on each week to see if there are any outliers within the data.
What I will need to do is be able to subtract a mean of all of the data from each weekly set of sales, then divide it by the standard deviation.
Current thought process for data:
If I can get the grand total at the bottom, could I get that as a value entered for each row in another column? Can I do it as a total average and a total standard deviation? I can do it outside of a pivot table, but I want something in a pivot table so it auto-populates.
Current Data
Desired Data
You can tackle this in at least two approaches:
Dynamic calculation using measures
Back-end calculation
The first approach consists of defining measures in the following context:
CALCULATE([MEASURES], All('Calendar'), VALUES('Calendar'[Year]), VALUES('Calendar'[Month]))
This allows you to calculate a measure in a context that consider the entire month. Therefore, for each day you would have a measure that gives you the stdev of the entire month.
Pro: dynamic; fast to implement; can be based on measures already defined
Cons: more calculation in front-end slows down your report
The second approach consists of pre-calculating this values in the back-end. Here you have two possible approaches:
Data source: add these new columns in the data source (e.g. Database)
Pro: best-practices and clean approach
Cons: static; cannot use measures already defined
Calculated Column in DAX: define the value as a Calculated Column in the back-end of Power BI using the same structured defined for the Measure:
CALCULATE([MEASURES], All('Calendar'), VALUES('Calendar'[Year]), VALUES('Calendar'[Month]))
Pro: fast to implement
Cons: static; really against best-practices
In Power BI I used following measures (replace 'stack' with 'your table name')
Total StdDev = CALCULATE(STDEV.P(stack[sum of sales]), ALL(stack))
TotalMean = CALCULATE(AVERAGE(stack[sum of sales]),ALL(stack))
Z score = (SUM(stack[sum of sales]) - [TotalMean])/[Total StdDev]
I used average to calculate MEAN and I get different result to yours (please see below).
If you can share formula that you used to calculate 'TotalMean' maybe I can update it.

Two different numbers after an average inside Google Big Query and Data Studio

I am averaging a number, grouped by week inside of Google Data Studio, and i am averaging the same numbers grouped by week inside of Big query however the output is slightly different.
Overall Score
AVG(table.score) OVER (PARTITION BY Weeknum) as OverallScore
The datasource is a list of scores, along with a date. I am averaging this inside DS using the aggregate function within the metric, and using the Time dimension ISO Year Week.
The purpose of this is to have one set of numbers hard coded, whilst the other line is used to filter to different departments, keeping the original "overall" score present to be used as a benchmark.
Exporting my table into excel, i can average it filtered by week 3 (See below) and i it returns 19.59 as well. Meaning, the avg aggregate function inside Datastudio is the same as excel. Also, i can query the table using the below, which rules out an averaging difference inside bigquery. However when i place overall score into the graph below i get slightly different numbers for the overall score..
SELECT avg(overallscore) FROM `dbo.table` where weeknum = '2018 3'
Output = 19.59
Does anyone have an idea what may be causing this?
When you open the report, you should be able to see the query it runs in your query history in Big Query. Check that it's using the same formula as sometimes it uses approximate aggregates.

Medians and slicers in DAX

I am having an issue that I am hoping some more experienced DAX programmers may be able to help me with. I have been trying to develop a dashboard in Excel 2013 / PowerPivot / PowerView and one of the graphics I would like to display is a line chart of median performance by hour of day. I would then like to filter the data set with my performance metrics based on a separate column, and link that to a slicer. The medians should be calculated relative to the filtered data set. For the median calculation I am trying to adapt the formula proposed by Marco Russo here (http://sqlblog.com/blogs/marco_russo/archive/2010/07/20/median-calculation-in-dax.aspx).
To illustrate the problem, suppose that I have two tables - main_table and other_table. Main_table has 4 columns- RowID, hour_of_day, performance_metric, and category. Other_table has two columns- hour_of_day and median_column. My goal is to find a formula for median_column such that it shows the median performance metric by hour of day, but can still be sliced by category. The formula I tried to use for the medians was
=CALCULATE(
MINX(
FILTER(
VALUES(main_table[performance_metric]),
CALCULATE(
COUNTA(main_table[performance_metric]),
main_table[performance_metric] <= EARLIER(main_table[performance_metric]))
> COUNTA(main_table[performance_metric]/2),
main_table[performance_metric]),
FILTER(
main_table,
main_table[hour_of_day] = EARLIER(other_table[hour_of_day])))
Or without formatting:
=CALCULATE(MINX(FILTER(VALUES(main_table[performance_metric]), CALCULATE(COUNTA(main_table[performance_metric]), main_table[performance_metric] <= EARLIER(main_table[performance_metric])) > COUNTA(main_table[performance_metric]/2), main_table[performance_metric]), FILTER(main_table, main_table[hour_of_day] = EARLIER(other_table[hour_of_day])))
However, when I create a slicer based on category in main_table, my chart does not seem affected by the slicer. My understanding was that by putting main_table as opposed to ALL(main_table) as the first argument in the last FILTER call, my median calculations would be subject to slices and filters applied to main_table. Am I missing something obvious here?
Calculated columns are apparently computed before queries are executed, therefore anything that needs to be affected by slicers must be entered as a measure, not a calculated column.
Answered in more detail here (http://www.mrexcel.com/forum/powerpivot-questions/741071-medians-context-issues-dax.html#post3641780)

Calculating Percent of Total in Power Pivot Model

I have created a power pivot table as shown in the picture. I want to calculate quarter over quarter sales change. For which I have to divide for example corporate family "Acer" 's sales in 2012Q4 by sum of all the corporate family. I am using calculated measure to do this, but I am not sure what formula I can use.
My need is to create two columns, one for 2012Q4 percent of total and one for 2013Q1 percent of total. Then I will create another measure to find the difference. So the formula for 2012Q4 should be like this 1624442 / (1624442+22449+1200+16123) . Any idea which function can help me do it?
It sounds like you are measuring the change in the percent of total for each corporate family from quarter to quarter. You will need to create 3 calculated measures. I'm not sure what your model looks like so I can't give you the exact formula, but here is the idea.
CurrentQtr%ofTotal:= Divide(Sum('Sales'[Units]),Calculate(Sum('Sales'[Units]), All['Product'[Corporate Family])))
PrevQtr%ofTotal:= DIVIDE(CALCULATE(Sum('Sales'[Units]), DATEADD(DimDate[DateKey], -1, QUARTER)),
CALCULATE(Sum('Sales'[Units]), DATEADD(DimDate[DateKey], -1, QUARTER), All('Product'[Corporate Family]))))
Change%ofTotal:= DIVIDE(([CurrentQtr%ofTotal]-[PrevQtr%ofTotal]),[PrevQtr%ofTotal])
I used the divide function because it handles divide by zero errors. You use the ALL function to remove the filter on the Corporate Family column from the filter context. The Change%ofTotal is just to find the differenc. I'm calculating % change but you may just want to subtract.
Here's the link to a good blog post on time intelligence. And here's one on calculating percent of total.
For percentages please follow the tutorial on the Tech on the Net.
Adding another column where you calculate a difference between two pivot columns will not work - this column is "unpivotable", as it relies on a column defintion. You would need to copy and paste pivot as values to another worksheet and do the extra calculation there.

Calculate % Change in Cognos Report Studio

Is it possible to calculate a percentage change in a Cognos Report?
Currently, I have a crosstab that has years as the columns and widgets in the rows with a calculation of total revenue. I am looking to calculate the annual % increase. New to Cognos, but I could accomplish this using other tools.
Any help will be very much appreciated.
Sorry for the late answer to your question but perhaps it will help others who see it.
It's kind of annoying, but it can be done. If you have one Query which uses an input parameter to select the year, perhaps it is a filter that says [Year] = ?YearPrompt?, then you can make a second Query which uses the filter [Year] = ?YearPrompt? - 1. Then, you can join these two queries, and the third query (made by joining the first two) will have both this year's revenue, and last year's revenue, available to it for a calculation, such as what the percent change is.
You need to create a query calculation item as one of the columns and in it you take Year2 - Year1(You can choose the exact columns you already have in the report using the Data Items tab when you are making the query calc) Also make sure you make this query calc a % in its format.

Resources