Power BI (DAX) - Month over Month Decliners Calculation - powerbi

Working in Power BI, I've created a couple of nested measures used to calculate month over month growth.
MoM $ Adj CM-1 = CALCULATE ([MoM $],
ALL ('Calendar'),
'Calendar'[Relative Month] = "CM-1",
'Group'[Flag] = "Yes",
'Type'[Type] = "N/A"
)
This works as expected. I am trying to create another measure based on the MoM $ Adj CM-1 measure to calculate the sum of only the negative values so I can see the top declining accounts.
Top Decliners = = CALCULATE ([MoM $],
ALL ('Calendar'),
'Calendar'[Relative Month] = "CM-1",
'Group'[Flag] = "Yes",
'Type'[Type] = "N/A",
[MoM $] < 0
)
This doesn't work (throws an error).
Question is it possible to use the existing measure with an additional filter to sum only the negative values so I can keep all existing filters? Or do I need to write the measure from the base table and reapply all the filters?

It is really hard to work on powerbi measures in stack overflow because we can't reproduce your work.
However, I believe you are trying to reference a measure in calculate which normally isn't allowed with out a filter wrapper.
Rather than trying to incorporate filter in your answer, may I suggest you create a new "root" measure of negative only MoM measure and then reference that measure in the same way as your first formula?
If you want to use filter then try the below. Depending on the base measure is calculated, you need to validate if the results are correct:
Top Decliners = CALCULATE ([MoM $],filter(
ALL ('Calendar'),
'Calendar'[Relative Month] = "CM-1",
'Group'[Flag] = "Yes",
'Type'[Type] = "N/A",
[MoM $] < 0)
)

Related

How to calculate the cumulative total of an average (calculated column)

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

HR Employee Count with Slicing by Dimensions - Slowly Changing Dimension

I need to calculate headcount while keeping the measure sliceable by any dimension connected to the fact table. Given the nature of my tables and model, what I need to do is a point in time calculation on a Slowly Changing Dimension Type 2.
I managed to make it work using the function KEEPFITLERS, but I need a more scalable function that wouldn't require me to list all the dimensions I want to slice by.
Here is my PowerBI file with sample data: https://gofile.io/d/smS2Hr
Here is a simplified sketch (image) of my model: https://ibb.co/fQYpsdx
Background:
The first measure I am calculating is the number of Employees at the Start of the Period (Employees SoP). If the end-user selects in PowerBI the whole month of January 2020, the Start of the Period is Jan 1st, 2020. Hence, "Employees SoP" for the month of Jan 2020 will give the number of employees on Jan 1st, 2020.
The formula below calculates the correct values for Employees SoP:
Employees SoP =
VAR MinDate = MIN ( 'Date'[Date]) //Mininum date selected by end-user in PowerBI
VAR Result =
CALCULATE (
DISTINCTCOUNT(Fact[EmployeeId]),
FILTER(ALL('Fact'), 'Fact'[EffectiveStartDate] <= MinDate
&& IF(ISBLANK('Fact'[EffectiveEndDate]), date(2050,1,1), Fact[EffectiveEndDate]) > MinDate
))
RETURN
Result
The problem with the formula above is that, because of the ALL function, the measure is not sliceable by any dimension, i.e., Pay Class and Employment Status (the same number repeats itself).
Results:
Hence, I created this other measure using KEEPFILTERS, and it works perfectly.
Employees SoP KEEPFITLERS =
VAR MinDate = MIN ( 'Date'[Date]) //Mininum date selected by end-user in PowerBI
VAR Result =
CALCULATE (
DISTINCTCOUNT(Fact[EmployeeId]),
FILTER(ALL('Fact'), 'Fact'[EffectiveStartDate] <= MinDate
&& IF(ISBLANK('Fact'[EffectiveEndDate]), date(2050,1,1), Fact[EffectiveEndDate]) > MinDate
), KEEPFILTERS(PayClass), KEEPFILTERS(EmploymentStatus))
RETURN
Result
The problem with this formula is that I have to list all the dimensions I want to slice by ( e.g., PayClass, EmploymentStatus) inside the DAX formula. This is not very scalable.
I did some experimenting with REMOVEFILTERS but it looks like it does not work with DirectQuery for now, so it wouldn't solve my production problem. Link:
https://learn.microsoft.com/en-us/dax/removefilters-function-dax
QUESTION:
How can I write this measure using an alternative to KEEPFILTERS with which I wouldn't have to list each dimension I want to slice by?
Thank you!
I just managed to solve my problem. I made both relationships to the Dates table "inactive". With that, I could remove the "ALL" function in the DAX formula and now it not only calculates the correct values, but it also slices nicely. I will have to use the USERELATIONSHIP function to calculate more complex measures but, in most of the cases, this simple solution works like a charm.
Employees SoP without ALL =
VAR MinDate = MIN ( 'Date'[Date]) //Mininum date selected by end-user in PowerBI
VAR Result =
CALCULATE (
DISTINCTCOUNT(Fact[EmployeeId]),
FILTER('Fact', 'Fact'[EffectiveStartDate] <= MinDate
&& IF(ISBLANK('Fact'[EffectiveEndDate]), date(2050,1,1), Fact[EffectiveEndDate]) > MinDate
))
RETURN
Result

Power BI - Daily difference as a value and percentage

I have a power BI report that has the following
Date, City, Town, Order number,
What i would like to do is create a report that shows the total orders (volume) for every day (which i can do as that is nice and easy) but I also wanted to show the difference from the previous reported day (some days we dont have data, eg bank holidays etc)
I am new to power bi and my technical skills are not brilliant.
Thank you in advance to anyone who is able to provide a solution.
Welcome to SO. There are a few ways of achieving it - you can even calculate these values directly in Power Query - it all depends on your data model and how the report itself is constructed.
Below are two solutions that you might want to consider:
Solution 1 - calculated column
This adds a new column to your table. The overall concept is to find the maximum date that is less than the current row's date and retrieve the respective value.
Volume t-1 =
var ThisDate = Table1[Date]
var PrevDate =
MAXX(FILTER(ALL(Table1[Date]), Table1[Date] < ThisDate), Table1[Date])
var PrevValue =
MAXX(FILTER(Table1, Table1[Date] = PrevDate), Table1[Current Volume])
return
PrevValue
You can now use this new column to calculate the difference between the current value and the previous value, e.g.:
Difference = [Current Volume] - [Volume t-1]
Solution 2 - measure
mVolume t-1 =
var ThisDate = MAX(Table1[Date])
var PrevDate =
MAXX(FILTER(ALL(Table1[Date]), Table1[Date] < ThisDate), Table1[Date])
var PrevValue =
MAXX(FILTER(ALL(Table1), Table1[Date] = PrevDate), Table1[Current Volume])
return
PrevValue
Similar to the first solution, you can now calculate the difference between this measure and the [Current Volume] field. However, the final formula will depend on your report and visualization filters. For example, if you add a table with Dates column (daily frequency), you can add the following measure to your table visualization:
[mDifference] = MAX(Table1[Current Volume]) - MAX(Table1[Volume t-1])
I hope this is a good starting point - good luck!

Power BI (DAX) Multiple Filter Sum Total

I am a new user of Power BI. I have a two step process which I need help with.
(1) - Calculate filtered (string) sum totals, which update based on slicers in report
(2) - Calculate ratio using outputs from Step 1 by quarter.
Data set approximates this:
The idea is that my report should be able to calculate Price per Liter if filtered by Color and if filtered by Shade (ie. multiple filters, but not restricted), by Quarter, so the trend can be plotted.
So far, I can calculate what I need using only one filter.
Total Price per quarter =
CALCULATE (
SUM ( Data[Price] ),
FILTER ( data, data[Quarter] = EARLIER ( data[quarter] ) ),
FILTER ( data, data[Colour] = EARLIER ( data[Colour] ) )
)
The trouble is that this approach doesn't seem to be so accurate when including more than one filter. From what I can gather it seems to double count Price (if say using two filters)
Total Price (TP) should essentially adjust for each filter (Quarter, Colour & Shade). If Q1 Selected TP = 2800 (1000+500+600+700); If Q1 & Green, TP = 1500 (1000+500); If Q1, Green & Light, TP = 1000. I know it is a simple problem, but I am not quite familiar with the functions and syntax just yet.
Can you please provide me with a DAX expression which might achieve this?
Just use the following measure without any filter (right click on your table and add measure):
Total Price = SUM(data[Price])
Now put that measure into a KPI visual and put the rest of your columns as slicers on the report. You will see if you select for example Q1 the KPI visuals value will change. The same goes for every filter combination you will choose.

DAX Cumulative Window Function

I have the simple dataset as below:
I need to permit summing the DistanceTraveled column in a "Measure" given date filters selected, and date order, to allow a cumulative total. The data model is dead simple as it only have one date dimension:
My DAX for the measure is:
Measure = CALCULATE(SUM(ActivityReport[DistanceTraveled]), FILTER(Timestamp,Timestamp[Timestamp] <= MAX(Timestamp[Timestamp])))
I know I must be missing something simple, how can I create a cumulative total given the filtered and increasing Timestamps for column DistanceTraveled?
I think you forgot to include all dates, Try this..
Measure = CALCULATE(
SUM(ActivityReport[DistanceTraveled]),
FILTER(ALL('Timestamp'[Timestamp]),
Timestamp[Timestamp] <= MAX(Timestamp[Timestamp])
)
)
What I ended up doing:
Measure = CALCULATE(
SUM(ActivityReport[DistanceTraveled]),
FILTER(ALLSELECTED(ActivityReport),
ActivityReport[Timestamp] <= MAX(ActivityReport[Timestamp])
)
)