Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
tongdai
Regular Visitor

Incorrect Power BI Dynamic Previous Period Calculation

I have a date slicer to control and update all the KPI ratios in my visual. When I choose a certain date range, one of my KPI ratios should show the total count within my selected date range (e.g Start: 2020-07-04, End: 2020-07-31). Another ratio should be showing the same period before (e.g Start: 2020-06-07, End: 2020-07-04). But in my current situation, I see both ratios are the same (current vs previous). Here is my DAX calculation for the previous period ratio

 

num_num_in_Previous_Period = 

VAR
    num_Start_Period = MIN(SN_Incident[sys_created_on])
VAR
    num_End_Period = MAX(SN_Incident[sys_created_on])
var
    num_Period_Diff = DATEDIFF( num_Start_Period, num_End_Period, DAY)
VAR 
    num_End_of_Previous_Period = num_Start_Period
VAR
    num_Start_of_Previous_Period = num_End_of_Previous_Period - num_Period_Diff
RETURN
    CALCULATE( SUM(SN_Incident[num]), FILTER(SN_Incident, num_Start_of_Previous_Period <= SN_Incident[sys_created_on])) 7 

 

d.png

1 ACCEPTED SOLUTION
Anonymous
Not applicable

// Such calculations should be performed with
// a Date table in the model. I suspect your
// model consists of one big table, right?
// If this is the case, then please be aware
// that you're not doing it right. To know
// how to correctly model data in Power BI
// please read upon dimensional modeling or
// watch some YT videos where such things
// are discussed at length. If you don't follow
// Best Practices, be prepared for some nasty
// bugs that you'll not even be able to
// diagnose. You'll not even be aware that
// they exist.

// But here's the measure you're asking for
// in this "dirty" model:

[Your Measure] =
VAR __start = MIN( SN_Incident[sys_created_on] )
VAR __end = MAX( SN_Incident[sys_created_on] )
VAR __dayDiff = DATEDIFF( __start, __end, DAY )
VAR __endPrev = __start
VAR __startPrev = __endPrev - __dayDiff
VAR __result =
    CALCULATE(
        SUM( SN_Incident[num] ),
        __startPrev <= SN_Incident[sys_created_on],
        SN_Incident[sys_created_on] <= __endPrev
    )
RETURN	
	__result

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

// Such calculations should be performed with
// a Date table in the model. I suspect your
// model consists of one big table, right?
// If this is the case, then please be aware
// that you're not doing it right. To know
// how to correctly model data in Power BI
// please read upon dimensional modeling or
// watch some YT videos where such things
// are discussed at length. If you don't follow
// Best Practices, be prepared for some nasty
// bugs that you'll not even be able to
// diagnose. You'll not even be aware that
// they exist.

// But here's the measure you're asking for
// in this "dirty" model:

[Your Measure] =
VAR __start = MIN( SN_Incident[sys_created_on] )
VAR __end = MAX( SN_Incident[sys_created_on] )
VAR __dayDiff = DATEDIFF( __start, __end, DAY )
VAR __endPrev = __start
VAR __startPrev = __endPrev - __dayDiff
VAR __result =
    CALCULATE(
        SUM( SN_Incident[num] ),
        __startPrev <= SN_Incident[sys_created_on],
        SN_Incident[sys_created_on] <= __endPrev
    )
RETURN	
	__result
Greg_Deckler
Super User
Super User

Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors