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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
Rt123
Frequent Visitor

Find totals for previous month based on selectedValue

I am trying to calculate totals for previous month (dont have date dimension table) with month names. By using the below formula, there is no error but its giving me blank value. e.g. if selected month = Feb (monthNum = 8), retreive Jan(monthNum = 8-1) totals.

 
Total Actuals_PM = SUMX(FILTER(L_Cube, (L_Cube[[Scenario]]] = "Actual") &&  (L_Cube[[Financial Year]]] = "2024") && L_Cube[MonthNum] = (SELECTEDVALUE(L_Cube[MonthNum])-1)), L_Cube[[Amount]]])
 
Much appreciated..
1 ACCEPTED SOLUTION
v-kongfanf-msft
Community Support
Community Support

Hi @Rt123 ,

 

The best way to do this entails creating creating a date table that is connected to your data table through a one-to-many relationship.

Date = 
ADDCOLUMNS (
    CALENDAR ( DATE ( 2023, 01, 01 ), DATE ( 2024, 12, 31 ) ),
    "Year", YEAR ( [Date] ),
    "Quarter", ROUNDUP ( MONTH ( [Date] ) / 3, 0 ),
    "Month", MONTH([Date]),
    "Week", WEEKNUM ( [Date] ),
    "Year-Qur",
        YEAR ( [Date] ) & "Q"
            & ROUNDUP ( MONTH ( [Date] ) / 3, 0 ),
    "Year-Mon",
        YEAR ( [Date] ) * 100
            + MONTH ( [Date] ),
    "Year-Week",
        YEAR ( [Date] ) * 100
            + WEEKNUM ( [Date], 2 ),
    "Weekday", WEEKDAY ( [Date], 2 )
)

vkongfanfmsft_0-1710235271590.png

vkongfanfmsft_1-1710235322558.png


Then with the following formula, you can use the SELECTEDVALUE function to dynamically get the last month's value.

result_ =
VAR sel_mon =
    SELECTEDVALUE ( 'Date'[Month] )
VAR max_ =
    CALCULATE ( MIN ( 'Date'[Date] ), FILTER ( 'Date', 'Date'[Month] = sel_mon ) ) - 1
VAR min_ =
    CALCULATE ( MIN ( 'Date'[Date] ), 'Date'[Month] = MONTH ( max_ ) )
RETURN
    CALCULATE ( SUM ( 'Table'[Value] ), DATESBETWEEN ( 'Date'[Date], min_, max_ ) )

vkongfanfmsft_2-1710235416566.png

vkongfanfmsft_3-1710235430958.png

 

Best Regards,
Adamk Kong

 

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

3 REPLIES 3
v-kongfanf-msft
Community Support
Community Support

Hi @Rt123 ,

 

The best way to do this entails creating creating a date table that is connected to your data table through a one-to-many relationship.

Date = 
ADDCOLUMNS (
    CALENDAR ( DATE ( 2023, 01, 01 ), DATE ( 2024, 12, 31 ) ),
    "Year", YEAR ( [Date] ),
    "Quarter", ROUNDUP ( MONTH ( [Date] ) / 3, 0 ),
    "Month", MONTH([Date]),
    "Week", WEEKNUM ( [Date] ),
    "Year-Qur",
        YEAR ( [Date] ) & "Q"
            & ROUNDUP ( MONTH ( [Date] ) / 3, 0 ),
    "Year-Mon",
        YEAR ( [Date] ) * 100
            + MONTH ( [Date] ),
    "Year-Week",
        YEAR ( [Date] ) * 100
            + WEEKNUM ( [Date], 2 ),
    "Weekday", WEEKDAY ( [Date], 2 )
)

vkongfanfmsft_0-1710235271590.png

vkongfanfmsft_1-1710235322558.png


Then with the following formula, you can use the SELECTEDVALUE function to dynamically get the last month's value.

result_ =
VAR sel_mon =
    SELECTEDVALUE ( 'Date'[Month] )
VAR max_ =
    CALCULATE ( MIN ( 'Date'[Date] ), FILTER ( 'Date', 'Date'[Month] = sel_mon ) ) - 1
VAR min_ =
    CALCULATE ( MIN ( 'Date'[Date] ), 'Date'[Month] = MONTH ( max_ ) )
RETURN
    CALCULATE ( SUM ( 'Table'[Value] ), DATESBETWEEN ( 'Date'[Date], min_, max_ ) )

vkongfanfmsft_2-1710235416566.png

vkongfanfmsft_3-1710235430958.png

 

Best Regards,
Adamk Kong

 

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

 

 

@v-kongfanf-msft there are several issues with the proposed calendar table.  Note that there is a QUARTER() function.  Note that WEEKNUM requires specification of the second parameter to be deterministic, same for Year-Week.

 

The last couple columns should all be generated via the FORMAT command, not via math.

 

 

lbendlin
Super User
Super User

you will want to invest in a calendar table.

 

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information or anything not related to the issue or question.

If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

Feb2025 NL Carousel

Fabric Community Update - February 2025

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

Top Kudoed Authors