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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

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
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.