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
EnrichedUser
Helper III
Helper III

Return the Last Date with Data by Group

Hi, 

I have a simple model with two tables.

Table A: Receipts
Table B: Date

I am wanting to write a measure that will return the Last Year Month Number that had a receipt for each month of the year. 

As an example
For Key AA, Feb 2023 the expected output is 24276 which is the prior year month of January where we had transactions in the receipt table.

 

Last Month with Receipts = 
VAR CurrentSiteItemKey = SELECTEDVALUE('Receipts'[Site_Item_Key])
VAR CurrentYearMonthNumber = SELECTEDVALUE('Date'[Year Month Number])

VAR LastMonthWithReceipts =
    CALCULATE(
        MAX('Date'[Year Month Number]),
        FILTER(
            ALL('Receipts'),
            'Receipts'[Eff Date] <= MAX('Date'[Date]) &&
            'Receipts'[Site_Item_Key] = CurrentSiteItemKey
        )
    )
RETURN
    IF( 
        ISBLANK(LastMonthWithReceipts),
        1, 
        LastMonthWithReceipts 
    )

 



Site_Item_KeyYear MonthYear Month NumberLast Unit Cost Month (current)ExpectedReceipt Data
AAJan-23242762427624276Yes
AAFeb-2324277 24276 
AAMar-23242782427824278Yes
AAApr-2324279 24278 
AAMay-2324280 24278 
AAJun-23242812428124281Yes
AAJul-23242822428224282Yes
AAAug-23242832428324283Yes
AASep-2324284 24283 
AAOct-23242852428524285Yes
AANov-2324286 24285 
AADec-2324287 24285 
BBJan-2324276   
BBFeb-2324277   
BBMar-2324278   
BBApr-2324279   
BBMay-2324280   
BBJun-23242812428124281Yes
BBJul-23242822428224282Yes
BBAug-2324283 24282 
BBSep-2324284 24282 
BBOct-2324285 24282 
BBNov-2324286 24282 
BBDec-2324287 24282 
1 ACCEPTED SOLUTION
v-rongtiep-msft
Community Support
Community Support

Hi @EnrichedUser ,

Please have a try.

Create a measure.

Measure =
VAR _3 =
    MAXX (
        FILTER (
            ALL ( 'Table' ),
            'Table'[Site_Item_Key] = SELECTEDVALUE ( 'Table'[Site_Item_Key] )
                && 'Table'[Year Month] < SELECTEDVALUE ( 'Table'[Year Month] )
                && 'Table'[Last Unit Cost Month (current)] <> BLANK ()
        ),
        'Table'[Year Month]
    )
VAR _1 =
    IF ( _3 = BLANK (), MAX ( 'Table'[Year Month] ), _3 )
VAR _1re =
    CALCULATE (
        MAX ( 'Table'[Last Unit Cost Month (current)] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Site_Item_Key] = SELECTEDVALUE ( 'Table'[Site_Item_Key] )
                && 'Table'[Year Month] = _1
        )
    )
RETURN
    IF (
        MAX ( 'Table'[Last Unit Cost Month (current)] ) <> BLANK (),
        MAX ( 'Table'[Last Unit Cost Month (current)] ),
        _1re
    )

 

vrongtiepmsft_0-1688092414663.png

How to Get Your Question Answered Quickly 

 

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

 

Best Regards
Community Support Team _ Rongtie

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

2 REPLIES 2
v-rongtiep-msft
Community Support
Community Support

Hi @EnrichedUser ,

Please have a try.

Create a measure.

Measure =
VAR _3 =
    MAXX (
        FILTER (
            ALL ( 'Table' ),
            'Table'[Site_Item_Key] = SELECTEDVALUE ( 'Table'[Site_Item_Key] )
                && 'Table'[Year Month] < SELECTEDVALUE ( 'Table'[Year Month] )
                && 'Table'[Last Unit Cost Month (current)] <> BLANK ()
        ),
        'Table'[Year Month]
    )
VAR _1 =
    IF ( _3 = BLANK (), MAX ( 'Table'[Year Month] ), _3 )
VAR _1re =
    CALCULATE (
        MAX ( 'Table'[Last Unit Cost Month (current)] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Site_Item_Key] = SELECTEDVALUE ( 'Table'[Site_Item_Key] )
                && 'Table'[Year Month] = _1
        )
    )
RETURN
    IF (
        MAX ( 'Table'[Last Unit Cost Month (current)] ) <> BLANK (),
        MAX ( 'Table'[Last Unit Cost Month (current)] ),
        _1re
    )

 

vrongtiepmsft_0-1688092414663.png

How to Get Your Question Answered Quickly 

 

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

 

Best Regards
Community Support Team _ Rongtie

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

amitchandak
Super User
Super User

@EnrichedUser , You can try this approch

Latest
https://amitchandak.medium.com/power-bi-get-the-last-latest-value-of-a-category-d0cf2fcf92d0

https://amitchandak.medium.com/power-bi-get-the-sum-of-the-last-latest-value-of-a-category-f1c839ee8...

 

or try LASTNONBLANKVALUE

 

calculate(LASTNONBLANKVALUE(Date[Date],SUM(Table[cost])), filter(all('Date'),'Date'[Year]= Max(Date[Year]) ))

 

Power BI closingbalancemonth closingbalancequarter closingbalanceyear, lastnonblankvalue- Closing Balance, Distibutre Target: https://youtu.be/yPQ9UV37LOU

 

DAX functions: lastnonblankvalue , firstnonblankvalue: https://www.youtube.com/watch?v=cN8AO3_vmlY&t=26940s

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.