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
webportal
Impactful Individual
Impactful Individual

How to express Today "one month ago"

Hello,

 

I want to create a card with last month's sales.

For that, I need a measure that is capable of expressing today "one month ago"

 

For example, this month's sales is:

 

This month sales = VAR ThisMonth = MONTH(TODAY()) RETURN CALCULATE('orders'[SalesAmount];'calendar'[month_number]=ThisMonth; 'calendar'[year]=2017)

All time intelligence functions seem to be good to handle columns of date but not scalar values like this case is. 

Also, because I'm using a card, there's no "filter context", therefore, I need a volatile function like TODAY.

 

Thanks!

1 ACCEPTED SOLUTION

Hi @webportal,

 

To consider the month January, how about try this:

Last month sales =
VAR LMonth =
    IF ( MONTH ( TODAY () ) = 1, 12, MONTH ( TODAY () ) - 1 )
VAR YearNum =
    IF ( MONTH ( TODAY () ) = 1, YEAR ( TODAY () ) - 1, YEAR ( TODAY () ) )
RETURN
    CALCULATE (
        SUM ( 'Previous month sales'[Sales] ),
        MONTH ( 'calendar'[Date] ) = LMonth,
        YEAR ( 'calendar'[Date] ) = YearNum
    )

Regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

7 REPLIES 7
r2db2
New Member

I got round this issue creating two addtonal column:

1st MonthsOpened = DATEDIFF(TABLE[Date],today(),MONTH)
2nd ThisReportMonth = IF(TABLE[MonthsOpened]=1,1,0)
 
I was after a record count so just returned a 1, but you could always display the sales value or a zero on the row then sum the "ThisReportMonth" column.
 
v-yulgu-msft
Employee
Employee

Hi @webportal,

 

Based on my test, measure similar to below could return last month sales dynamically.

Last month sales =
VAR LMonth =
    MONTH ( TODAY () ) - 1
RETURN
    CALCULATE (
        SUM ( 'Previous month sales'[Sales] ),
        MONTH ( 'calendar'[Date] ) = LMonth,
        YEAR ( 'calendar'[Date] ) = 2017
    )

So, what is your concern? As you mentioned, almost time intelligence functions supports date column rather than scalar values like Today(). This is by design, but you could use above workaround.

 

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hello,

Thanks, will it also work if today is in January?

What will LMonth be then?

Hi @webportal,

 

To consider the month January, how about try this:

Last month sales =
VAR LMonth =
    IF ( MONTH ( TODAY () ) = 1, 12, MONTH ( TODAY () ) - 1 )
VAR YearNum =
    IF ( MONTH ( TODAY () ) = 1, YEAR ( TODAY () ) - 1, YEAR ( TODAY () ) )
RETURN
    CALCULATE (
        SUM ( 'Previous month sales'[Sales] ),
        MONTH ( 'calendar'[Date] ) = LMonth,
        YEAR ( 'calendar'[Date] ) = YearNum
    )

Regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @v-yulgu-msft

 

Thank you so much for your assistance.

 

Happy New Year!

Ashish_Mathur
Super User
Super User

Hi,

 

Even if you do not have a Date column in your base data but simple Month and year columns, we can always create a date column from these 2 columns.  This will then allow us to use the Date/Time Intelligence functions.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
webportal
Impactful Individual
Impactful Individual

I found out this works:

 

Last month sales = VAR LastMonth = MONTH(TODAY()-DAY(TODAY()) RETURN CALCULATE('orders'[SalesAmount];'calendar'[month_number]=LastMonth; 'calendar'[year]=2017)

Although probably it's not the most correct syntax.. 

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.