Calculate Current and Previous month's Value based on slicer selection in power bi - powerbi

I have two tables called
Main Table contains the following data(Pic) and
Date table created using the dates from the Main table.
I want to calculate two fields/measures based on Date slicer selection from the Date Table
current month's revenue
previous month's revenue
Example: If I selected the 4th Month then it should sum distinct revenue of client A and B for the 4th month as current_month_revenue and Sum distinct revenue of A and B for 3rd month as previous_month_revenue.
I tried writing the following Measure to Calculate current_month_revenue and it is working fine but it is not giving the correct result for Previous_month_revenue.
I am getting the same value for the Previous month as well.
'Measure Previous Month Revenue' =
IF (
ISFILTERED ( 'Date'[Year_Month] ),
VAR myTable =
SUMMARIZE (
'Main Table',
'Main Table'[ClientName],
'Main Table'[Mon],
'Main Table'[Revenue]
)
RETURN
CALCULATE (
SUMX (
myTable,
'Main Table'[Revenue]
),
FILTER (
'Main Table',
'Main Table'[Mon]
= SELECTEDVALUE ( 'Date'[Month] - 1 )
)
),
VAR myTable =
SUMMARIZE (
'Main Table',
'Main Table'[Revenue],
'Main Table'[Mon],
'Main Table'[Revenue]
)
RETURN
SUMX (
FILTER (
myTable,
'Main Table'[Mon]
= MONTH (
TODAY ()
) - 1
),
'Main Table'[Revenue]
)
)
Desired Output
If 4th month is selected
Current Moth revenue = 100 + 200 = 300
Previous Month = 100+200 = 300
In this case, both are the same but in actual data, revenue is different for each month.

I see from the code that the 'Date' table has a numeric column called Month, that I assume to be of the same type as the Mon column in your 'Main Table'.
Also, since in the 'Main Table' there is no Year, I assume that the Year is not to be considered.
From the slicer over the Date table we can directly get the selected 'Date'[Month] using SELECTEDVALUE(). As default parameter we use the current month obtained by the TODAY() function.
Then we obtain the Previous Month subtracting one from the Selected Month and we can use it to slice the table grouped by CustomerName, Mon and Revenue. Grouping is needed to remove duplicate Revenues for the same customer on the same month and is implemented using SUMMARIZE()
As a final step we can aggregate the 'Main Table'[Revenue] of the filtered and grouped table using SUMX.
'Measure Previous Month Revenue' =
VAR CurrentMonth =
MONTH(
TODAY()
)
VAR SelectedMonth =
SELECTEDVALUE(
'Date'[Month],
CurrentMonth
)
VAR PrevMonth = SelectedMonth - 1
VAR MyTable =
CALCULATETABLE(
SUMMARIZE(
'Main Table',
'Main Table'[ClientName],
'Main Table'[Mon],
'Main Table'[Revenue]
),
'Main Table'[Mon] = PrevMonth,
REMOVEFILTERS( 'Date' )
)
VAR Result =
SUMX(
MyTable,
'Main Table'[Revenue]
)
RETURN
Result
The same code but for the Previus Month calculation can be written for the current month
'Measure Current Month Revenue' =
VAR CurrentMonth =
MONTH(
TODAY()
)
VAR SelectedMonth =
SELECTEDVALUE(
'Date'[Month],
CurrentMonth
)
VAR MyTable =
CALCULATETABLE(
SUMMARIZE(
'Main Table',
'Main Table'[ClientName],
'Main Table'[Mon],
'Main Table'[Revenue]
),
'Main Table'[Mon] = SelectedMonth,
REMOVEFILTERS( 'Date' )
)
VAR Result =
SUMX(
MyTable,
'Main Table'[Revenue]
)
RETURN
Result
A better solution could be implemented setting a relationship between the 'Date' table and 'Main Table'.
Depending on the business requirement, it could be possilbe to use a Date table at the month level granularity, with a YearMonth column instead of Mon, or at the Day level, with a Date column instead of the Mon column.

CALCULATE does not affect variables that you've already defined, so FILTER does nothing to the first SUMX. See this related post for a bit more detail.
I'd suggest writing the measure much more simply. Something like this:
Previous Month Revenue =
VAR PrevMonth =
SELECTEDVALUE (
'Date'[Month],
MONTH ( TODAY () )
) - 1
RETURN
CALCULATE (
SUM ( 'Main Table'[Revenue] ),
'Main Table'[Mon] = PrevMonth
)

Related

Power BI: Is there a way to get the a value based on the MAX date?

I am trying to make a calculated column in Power BI to return the last Job Title held for the name listed based on the maximum/latest date.
Current Title = VAR MaxDate =CALCULATE (MAX('Terms FY23'[Employee History.Date])) VAR EEID = CALCULATE(MAX('Terms FY23'[Employee History.Employee]))RETURN CALCULATE(FIRSTNONBLANK(SELECTCOLUMNS(FILTER('Terms FY23','Terms FY23'[Employee History.Date] = MaxDate && 'Terms FY23'[Employee History.Employee] = EEID),"JobTitle",'Terms FY23'[Employee History.Title]),_MaxDate))
I tried this but it returned the same exact Job Title column.
Date Full Name Job Title
10/31/2022 John Adams Landscaper I
11/30/2022 John Adams Crew Leader
12/31/2022 John Adams Crew Leader
1/31/2023 John Adams Crew Leader
2/15/2023 John Adams Crew Leader
For you sample data this one works
Last Job Title =
VAR MAXDATE =
MAXX('Table', 'Table'[Date])
RETURN
CALCULATE(
MAX('Table'[Job Title]),
FILTER(
ALL('Table'),
'Table'[Date] = MAXDATE
)
)
In case you have more than one distinct name in the table can go via this
calculated table
Lookup Table =
SUMMARIZE(
'Table',
'Table'[Full Name],
"Last Job Titel", CALCULATE(
MIN('Table'[Job Title]),
'Table'[Date] = MAX('Table'[Date])
)
)
to end up with this calulated column
Last Job Title=
LOOKUPVALUE(
'Lookup Table'[Last Job Titel],
'Lookup Table'[Full Name], 'Table'[Full Name]
)
You can add a calculated column that does this directly using the new INDEX windowing function as a filter argument, that uses ALLEXCEPT on your table to provide the correct row context:
Last Title =
CALCULATE (
SELECTEDVALUE ( 'Terms FY23'[Employee History.Title] ),
INDEX (
1,
ALLEXCEPT ( 'Terms FY23', 'Terms FY23'[Employee History.Name] ),
ORDERBY ( [Employee History.Date], DESC )
)
)
This type of calculation would normally require to store the maximum date value first, and then solve for the title:
Last Title Old =
VAR _max_date =
CALCULATE (
MAX ( 'Terms FY23'[Employee History.Date] ),
ALLEXCEPT ( 'Terms FY23', 'Terms FY23'[Employee History.Name] )
)
RETURN
CALCULATE (
SELECTEDVALUE ( 'Terms FY23'[Employee History.Title] ),
ALLEXCEPT ( 'Terms FY23', 'Terms FY23'[Employee History.Name] ),
'Terms FY23'[Employee History.Date] = _max_date
)
NB! If you have two people in this table with the same name, you are out of luck. You would need a more robust way of handling employees, for example by using a unique employee ID instead of some arbitrary name.

power bi - ranking on a date

I tried to make a ranking on a date so that every beginning of a month it resets but I can't get it. the furthest I got is that every month simply gets an cumulative ranking based on RANKX on power bi which includes a month and a year.
Has anyone needed such a ranking and succeeded?
A generalised set-up for a Calculated Column would be along the lines of:
=
VAR ThisMonth =
MONTH( Table1[Date] )
RETURN
RANKX(
FILTER( Table1, MONTH( Table1[Date] ) = ThisMonth ),
Table1[Date],
,
ASC
)

Running total with a slicer in Powerbi

I am trying to find the running total by month, it worked using the following measure but only without using a slicer for the category "BLK":
With Date = VAR MaxDate =
MAX ( 'Calendar Table'[Date] ) RETURN
CALCULATE (
SUM ( 'Injuries'[Total] ),
FILTER (
ALL ( 'Injuries'[Classification] ),
'Injuries'[Classification] = "FAC"
),
FILTER ( ALLSELECTED ( 'Calendar Table' ), 'Calendar Table'[Date] <= MaxDate)
)+0
The result was only accurate when I don't apply the Category slicer "BLK", I tried a different measure:
With BLK = CALCULATE( SUM('Oxy Oman Personal Injuries'[Total]),
FILTER (
ALL ( 'Injuries'[Classification] ),
'Injuries'[Classification] = "FAC"
), ALLSELECTED('Blocks'[BLK]) )+0
The above gave me something similar to the actual data, it did not sum up the numbers.. at least it gave me the correct total of a the category selected = 5 while the first measure gave the total wrong = 4.
Here is a screenshot of the results for both measures with and without a slicer:
Without BLK slicer:(With Data gave me what I wanted)
Using one Category in the BLK Slicer:
How can I fix that?

Dax Counts at end of each month

I have a measure that depending on a "before" date slicer shows how many accounts were active at any given point in the company's history. I'm being asked to show month over month growth (end of month 1 compared to end of month 2 totals) but that's difficult given my measure needs a date slicer with one date value to return a total.
Active_Accounts =
CALCULATE (
COUNTX (
FILTER (
VALUES ( 'TEST CHARGES'[BI_ACCT] ),
[total as of date] > 0
),
[BI_ACCT]
)
)
link to sample file
https://www.dropbox.com/s/pewpm85wogvq3xf/test%20active%20charges.pbix?dl=0
if you move the slider you'll see the active accounts total change to show at that time in history how many accounts had an active charge. What I'm hoping to add to the dashboard is a measure that can be placed on a table of month end values and show the active accounts at that time so I can do month to month comparisons.
Active Accounts =
var month_end =
ENDOFMONTH (
LASTNONBLANK (
'Test Charges Date Table'[Date],
CALCULATE ( DISTINCTCOUNT( ( 'TEST CHARGES'[BI_ACCT] ) )
)
)
)
var last_date =
CALCULATE(
LASTNONBLANK('TEST CHARGES'[CHG_DATE], ""),
'TEST CHARGES'[CHG_DATE] <= max('Test Charges Date Table'[Date])
)
var num_of_actives =
CALCULATE(
Countx(
Filter(
Values('TEST CHARGES'[BI_ACCT]),
[total as of date] > 0
) , [BI_ACCT]
),
last_date <= month_end
)
return num_of_actives
As Peter advices you do not need Calculate() to show total in the card and using of Calculate() reduces speed of calculation. In your case speed is reduced with no reason.
There are no need to have a special column for month - use date hierarchy for row and just exclude day and quater levels.
Then add the measure to the visual table/matrix
Cummulative Count =
Calculate(
[Active_Accounts]
,'Test Charges Date Table'[Date]<=MAX('Test Charges Date Table'[Date])
)
Cummulative Count prevMonth =
Calculate(
[Cummulative Count]
,PARALLELPERIOD('Test Charges Date Table'[Date],-1,MONTH)
)

Problem on using ALLEXCEPT to get percentage

I have a problem of getting the percentage on customer sales in matrix table thta is sliced by store_id and employee_id using ALLEXCEPT function. The percentage of customer sales need to obrain from dividing customer sales measure in current filter by the same measure of the respective store_id, instead of total customer sales of all the store_id.
Here is my expectation of the output and the DAX when using ALL function on the staff_id.
% of Customer Sales =
VAR ALLExceptSales =
CALCULATE(
[Customer Sales],
ALL(
'Employee Lookup'[staff_id]
)
)
VAR Ratio =
DIVIDE(
[Customer Sales],
ALLExceptSales,
BLANK()
)
RETURN Ratio
I have tried using ALLEXCEPT on store_id from sales by store table, ALLEXCEPT store_id from store lookup table and ALLEXCEPT on both the tables, still giving me different output.
Here is the pbix file for testing.
https://drive.google.com/file/d/1fRrfsikHl9aK06GzAozJ9Wc16Ue0YJm2/view?usp=sharing
Anyone can hint me on?
Replace DAX with following
% of Customer Sales =
VAR ALLExceptSales =
CALCULATE(
sum(Table[Customer Sales]),
ALLEXCEPT(Table, 'Table'[store_id])
)
VAR Ratio =
DIVIDE(
sum(Table[Customer Sales]),
ALLExceptSales,
BLANK()
)
RETURN Ratio
Have you tried ALLSELECTED instead of ALLEXCEPT?
% of Customer Sales =
DIVIDE (
[Customer Sales],
CALCULATE ( [Customer Sales], ALLSELECTED ( 'Employee Lookup'[staff_id] ) )
)