Forum Discussion

ChristianDGreat's avatar
ChristianDGreat
Resolver I
3 years ago
Solved

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

  • johnt75's avatar
    johnt75
    3 years ago

    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] )
    

3 Replies

  • 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
    
    • ChristianDGreat's avatar
      ChristianDGreat
      Resolver I

      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.

       

      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.

       

      • johnt75's avatar
        johnt75
        Super User

        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] )