I needed to replace this measure:
CALCULATE([GM % YTD], SAMEPERIODLASTYEAR('Date'[Date]))
By this one:
VAR VAR1 = ADDCOLUMNS( VALUES(Revenue[Key_Client]),
"Col1", CALCULATE([GM % YTD], SAMEPERIODLASTYEAR('Date'[Date]),
REMOVEFILTERS(Revenue[Type],Revenue[SectorType]))
)
RETURN AVERAGEX(VAR1, [Col1])
Both measures point to GM % YTD, which is:
CALCULATE([GM %], DATESYTD('Date'[Date],"31/05"))
I get this, when I display them side by side:
The values are ok, my problem is with the Total. I am unable to find how/where is the aggregation on the left column done... How is that 73,2% achieved? It doesn't seem to be average...
Also… how can I force the measure on the right to do the same aggregation?
In the ADDCOLUMNS version, you are iterating over each Revenue[Key_Client] and only averaging after the [GM % YTD] has been calculated for each one separately. For a single client, there's only one thing to average, so the value isn't affected by that step.
Generally, you want to compute the measure over all clients together rather than averaging the individual numbers together to get a standard weighted average rather than an average where all clients are weighted equally.
Related
I'm having a hard time to calculate the running / cumulative total of average values...
My idea is to emulate a sales campaign for a product that never had one, using the average by day of all the products from an specific brand; problem is the code I made is only repeating the numbers from the average, it's not summing them up:
This is how it's now:
This is how it should be:
I managed to filter the values I want to calculate the average for, and that's for an specific brand (this is not going to be replicated to all products, and in the end I want to merge this in another calculated column which will calculate the goal for all the products (including the exception, which are the ones that never had a sales campaign before).
here's the code for the average:
AllPlutoProductsAverage = MAXX(FILTER('f_Leads(teste2)', [percentage] = EARLIER(d_CT_Leads_RT[percentage]) && 'f_Leads(teste2)'[group_name]="Pluto"), 'f_Leads(teste2)'[registers_per_day] )
And here's the code for the cumulative/running total column:
VAR _NoCampaign_RT =
CALCULATE(
MAX( 'd_CT_Leads_RT'[AllPlutoProductsAverage ] ) ,
FILTER( 'f_Leads(teste2)' ,
AND( 'f_Leads(teste2)'[group_name] = d_CT_Leads_RT[group_name] ,
'f_Leads(teste2)'[course] = d_CT_Leads_RT[course]
) &&'f_Leads(teste2)'[course_closed_percentage] = d_CT_Leads_RT[percentage]
)
)
Any ideas on how I fix that...?
Thanks in advance!! :))
I tried:
quick measure for running totals, didn't work
custom code for running totals, still repeated the values
creating a separate measure for average and then mentioned it on the column's running total code, same result
tried building up a running total code by adding the average calculation into it as a variable, didn't work either
tried exporting the calculation to a measure, didn't work either
And by 'didn't work' I mean the column No_PreviousCampaign still shows the repeated values from AllPlutoProductsAverage
I have a "Line and clustered column chart" in Power BI where the bars are actual values and line is budgeted values. Actual and budgeted values often vary enough that their Y axis scaling doesn't match, so I need both axes to have the same scale. Start value for both could be zero, but End needs to be calculated. We need to dynamically calculate End based on the visible data within the chart. My approach so far has been to create a measure that calculates the highest monthly actual value and another measure that calculates the highest monthly budget value, then take that max of those two measures and use it in a function for the "End" value of both Y axes. I have not found the DAX that works yet.
Example of y axes with different scaling and the max value we want
I suspect I'll need to get the min and max dates that are selected in the visual, and the following measures give me those:
MinSelectedDate = CALCULATE(MIN('Date'[CalendarDate]), ALLSELECTED('Date'))
MaxSelectedDate = CALCULATE(MAX('Date'[CalendarDate]), ALLSELECTED('Date'))
I also suspect I'll need to build a table within the measure and summarize the values then iterate over that table to find the max. Somthing like this:
MaxActual = MAXX(
SUMMARIZE(ALLSELECTED('FactActualValues'), 'Date'[FirstDayOfMonth], "Volume", [Actual Values]),
[Actual Volumes]
)
However, when I try this DAX the data ends up being sliced by the month within each year. I'm using a matrix for dev/test.
Dev/test using matrix
The optimal solution is to have an analytical model with two columns (Date and FY).
Cancel dynamization of the table, setting the years to the "Year" column and converting it to Text type
Create the column "Month" from the month number of "MonthName" and convert to type Text
Create the date column of the format dd-mm-yyyyy from the concatenation using "01/"+[Month]+"/"+[Year]
Format the new date column to date format
The y-axis scale is resolved by the "Clustered Column Chart" visualization as it takes the maximum value of a single column of values plus some margin at the top and takes that as the y-axis value.
Got it! I was very close but didn't need to calculate min or max dates. Using ALLSELECTED gave me just the records from the FactActualValues table that I needed without applying an explicit filter. I also learned that some functions (e.g. - MAX) cannot "see" columns returned by table functions, but others like MAXX do support it. SUMMARIZE allows you to create a column and I learned that some people use the empty single quote to reference such a column.
Here's the DAX that works:
MaxMonthlyActual =
MAXX(
SUMMARIZE(
ALLSELECTED('FactActualValues'),
'Date'[FirstDayOfMonth],
"#ActVol", SUM('FactActualValues'[Total])
),
''[#ActVol]
)
Here is what the chart looks like using the DAX measure:
Chart with Y axes with matching scales
I am trying to get my head around DAX and am struggling. I have a PowerBI Matrix in which I need to calculate the average of a measure. The measure is '% of population' and on the surface it appears to work exactly as expected.
It calculates correctly in the top matrix for the two levels and also summarises correctly in the bottom table.
As an example, I have highlighted in red the order of calculations for "A3"
For the record the % population is set to
% of Population = sum(Data[Value])/sum('Level'[Population])
The problem occurs when I filter on the Country and only select Country 2...
Country 2 does not have data for "D13". Although the Values sum up correctly (170), the Sum of the Population includes the 300 from the missing D13 row making a total of 600 and the '% population' of 28.33% (instead of 170 / 300 = 57%)
I am happy to turn off the group totals in the top grid so that the 28.33 does not show; so my real problem is actually with the bottom grid.
I think I need a new measure to be displayed in the bottom grid. I think it simply needs to sum up the values and divide by the sum of the populations - but only when the value is present. How do I do that?
Or am I totally on the wrong track and there is an obvious answer that I am missing?
The PowerBI file can be downloaded from here
Thanks in advance.
The reason this is happening is that the Country table does not filter the Level table in the relationship diagram since they both only filter one way to the Data table and there are no other relationships.
Without changing your data model, one way to fix this in DAX is to specify that you only want to count Population where Level[LevelId] matches a Data[SecondLevelId] in your current filter context.
Population =
DIVIDE (
SUM ( Data[Value] ),
CALCULATE (
SUM ( 'Level'[Population] ),
'Level'[LevelId] IN VALUES ( Data[SecondLevelId] )
)
)
I am trying to aggregate the following values (NHS, Social Care and Both B) by the reasons for delays column so i can find the reason with the highest value (from the 3 combined values named above).
I have tried using summarize to create a table with just the reasons for delays ,NHS, Social Care and Both B columns. By doing this i hoped i could create a column named totals which adds the NHS, Social Care and Both B Columns together in this summarized table thus giving me the total values for each reason for delay.
Though when i tried to run a maxx function around my totals column it seems to give me the wrong values.
I have tried wrapping my table with the distinct function so it aggregates all the columns in my summarize together, but this did not help either.
Max Delays =
MAXX (
SUMMARIZE (
csv,
csv[Reason For Delay],
csv[NHS],
csv[Social Care],
csv[Both B],
"totals", CALCULATE ( SUM ( csv[NHS] ) + SUM ( csv[Both B] ) + SUM ( csv[Social Care] ) )
),
[totals]
)
The smaller table (which should represent the summarized table) in the above picture with the total column shows the values i expect to carry my max calculation over, where i expect the max value to be 277.
The max value i am getting instead is 182. This is the max value in the unsummarized table below where i have multiple duplicates of my reasons for delay column and 182 is the highest value.
I have uploaded a sample of the pbix file i am working on if it may be of help;https://www.zeta-uploader.com/en/1184250523
First, create a measure for total reasons:
Total Reasons = SUM(csv[NHS]) + SUM(csv[Both B]) + SUM(csv[Social Care])
Second, create a measure for the max reason:
Max Reason = MAXX( VALUES(csv[Reason For Delay]), [Total Reasons])
Result:
How it works:
The first measure is for convenience. You can re-use it in other formulas, making code cleaner;
In the second measure, we create a list of distinct reasons using VALUES. Then MAXX iterates this list, calculates total for each reason, and then finds the largest of them.
Charts and visuals on Power BI can be filtered to the top n records for easier at-a-glance reporting. However, if I want to report the top and bottom records (eg: top 10 and bottom 10 dollar amounts) I need to place two visuals. This consumes more report space and breaks the cohesion of the page.
Is there a way to select two data subsets in one graph for this kind of reporting?
Here is the sample data I threw together.
On top of that, I created a simple measure for the Total Amount.
Total Amount = SUM(Data[Amount])
With that I created a new measure that will essentially flag each row as being in the Top or Bottom 3 (you can change the number to meet your needs).
This measure first checks if there is a value for Total Amount and "removes" any that have a blank value ("removes" by making the flag blank and thus will never be included in any filtering or such).
TopBottom =
IF(
ISBLANK([Total Amount]),
BLANK(),
IF(
RANKX(ALL(Data), [Total Amount], , ASC) <= 3 || RANKX(ALL(Data), [Total Amount], , DESC) <= 3,
1,
0
)
)
Once you have the ranking flag measure, you can add it to your visual and then filter to where the measure is 1.
Once that is all finished, you should have a visual only showing the entries you care about. Here is the full list of data with the flag visible and the resulting table when applying the filter.
Consider this as a slight improvement to the accepted answer. With this one, you don't need to change the formula whenever you want to change how many you want to see.
The only control you will need to change is the filter.
RankTopBottom = RANKX(ALL(Data), [Total Amount], , ASC) *
RANKX(ALL(Data), [Total Amount], , DESC)
It uses basically the same principle of the accepted answer, but instead of using an IF, we multiply both rankings. The smallest values will be the edges, the gratest values will be the middle.
So, when filtering, use "bottom" N, and pick an even number. (Or add a negative sign if you want the "top" N instead)
A quick result of multiplying inverse ranks: