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
ChristianDGreat
Resolver I
Resolver I

Last QTY based on selected date

Hello would it be okay if you can help me on this? I got stucked

 

So I have this table

ProductTimestampQty
A1-Jan23
A2-Jan123
A3-Jan123
A4-Jan213
B1-Jan123
B2-Jan123
B4-Jan123
C1-Jan10
C2-Jan11
C4-Jan13

 

and the result that im looking for is this
Filter Timestamp = 1/3

ProductLast Qty
A123
B123
C11


Explanation : So what I'm looking for is I want to get the latest qty by Product depending on what I filter.
So If I filter 1/3/2023 it will only scan below or equal to that date.
A = 123 from 1/3/2023

B = 123 from 1/2/2023

C = 11 from 1/2/2023

1 ACCEPTED SOLUTION

Try

Qty as at date =
VAR MaxDate =
    MAX ( 'Date'[Date] )
VAR SummaryTable =
    ADDCOLUMNS (
        SUMMARIZE ( 'Table', 'Table'[Product], 'Table'[Subtype] ),
        "@qty",
            SELECTCOLUMNS (
                CALCULATETABLE (
                    TOPN ( 1, 'Table', 'Table'[Timestamp], DESC ),
                    'Date'[Date] <= MaxDate
                ),
                "@qty", 'Table'[Qty]
            )
    )
RETURN
    SUMX ( SummaryTable, [@qty] )

View solution in original post

3 REPLIES 3
johnt75
Super User
Super User

You can create a measure like

Qty as at date =
VAR MaxDate =
    MAX ( 'Date'[Date] )
VAR Qty =
    SELECTCOLUMNS (
        CALCULATETABLE (
            TOPN ( 1, 'Table', 'Table'[Timestamp], DESC ),
            'Date'[Date] <= MaxDate
        ),
        "@qty", 'Table'[Qty]
    )
RETURN
    Qty

@johnt75 thanks, but when I add my full table it seems its only working if we are only looking at one column (product)
Anyway

What if this is the table, I added a subtype

ProductTimestampSubtypeQty
A1-JanA-123
A2-JanA-1123
A3-JanA-2123
A4-JanA-2213
B1-JanB-1123
B2-JanB-1123
B4-JanB-2123
C1-JanC-110
C2-JanC-111
C4-JanC-313

 

This is the error I got.

ChristianDGreat_0-1676307882563.png

 

The result im looking for is 

ProductQtyexplanation
A246A-1 (jan 2) = 123 A-2 (jan3) = 123 Total = 246
B123B-1(jan 2) = 123 Total = 123
C11C-1(jan2) = 11 Total = 11


Let me know if it makes sense.

 

Try

Qty as at date =
VAR MaxDate =
    MAX ( 'Date'[Date] )
VAR SummaryTable =
    ADDCOLUMNS (
        SUMMARIZE ( 'Table', 'Table'[Product], 'Table'[Subtype] ),
        "@qty",
            SELECTCOLUMNS (
                CALCULATETABLE (
                    TOPN ( 1, 'Table', 'Table'[Timestamp], DESC ),
                    'Date'[Date] <= MaxDate
                ),
                "@qty", 'Table'[Qty]
            )
    )
RETURN
    SUMX ( SummaryTable, [@qty] )

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.