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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
noliverte
Helper III
Helper III

Total previous period dynamically

Hello,

 

I have the following measure :

Total Session LY all day = CALCULATE([Total Sessions-all];DATESBETWEEN(Calendrier[Calendar_Date];date(2019;01;01);date(2019;03;08)))))

 

--> It works fine but in a static way...

I'd like to replace date(2019;01;01) by the first day of the previous year

and date(2019;03;08) by today's date...

 

I also tried 

Total Sessions LY-all = CALCULATE([Total Sessions-all];SAMEPERIODLASTYEAR(Calendrier[Calendar_Date]))

But Total Session LY-all include until the end of march and not the today's date...

 

Any help is appreciated.

 

Thanks

 

1 ACCEPTED SOLUTION

Thanks a lot it works fine, I jest adapt your code to fit to my problem.

 

Total Session LY all day = CALCULATE([Total Sessions-all];
DATESBETWEEN(Calendrier[Calendar_Date];
DATE(YEAR(TODAY())-1;1;1);
DATE(YEAR(TODAY())-1;MONTH(TODAY());DAY(TODAY()-1))
)
)
 
Have a nice day
 
Noliverte

 

View solution in original post

4 REPLIES 4
v-alq-msft
Community Support
Community Support

Hi, @noliverte 

 

Based on your description, I created data to reproduce your scenario.

Table:

d1.png

 

Calendar(a calculated table):

 

Calendar = CALENDARAUTO()

 

 

Then you may create a measure as follows.

 

Total Session LY all day = 
CALCULATE(
    SUM('Table'[Sessions]),
    DATESBETWEEN(
        'Calendar'[Date],
        DATE(YEAR(TODAY())-1,1,1),
        TODAY()
    )
)

 

 

Today is 3/10/2020. So the measure doesn't include sessions for 3/11/2020 and 3/31/2020. Here is the result:

d2.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

Thanks a lot it works fine, I jest adapt your code to fit to my problem.

 

Total Session LY all day = CALCULATE([Total Sessions-all];
DATESBETWEEN(Calendrier[Calendar_Date];
DATE(YEAR(TODAY())-1;1;1);
DATE(YEAR(TODAY())-1;MONTH(TODAY());DAY(TODAY()-1))
)
)
 
Have a nice day
 
Noliverte

 

Greg_Deckler
Super User
Super User

See if my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TIT...

 


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
amitchandak
Super User
Super User

With time intelligence and date calendar you can try

YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(('Date'[Date]),"12/31"))
This Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD((ENDOFYEAR('Date'[Date])),"12/31"))

Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"))
Last YTD complete Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(ENDOFYEAR(dateadd('Date'[Date],-1,Year)),"12/31"))
Last to last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-2,Year),"12/31"))

Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))

 

In case you do not want to use date slicer

-- Force today filter
YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(('Date'[Date]),"12/31"),'Date'[Date]<=today())

Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"),'Date'[Date]<=date(year(today()-1),month(today()),day(today())))

-- with filter

YTD Sales 1= CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(('Date'[Date]),"12/31"))

Last YTD Sales 1 = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"))

--combine

final YTD = if(isfiltered(Date[Month Year]) || isfiltered(Date[Year]) || isfiltered(Date[Date]),[YTD Sales 1],[YTD Sales])
final LYTD = if(isfiltered(Date[Month Year]) || isfiltered(Date[Year]) || isfiltered(Date[Date]),[Last YTD Sales 1],[Last YTD Sales])

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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