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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
EZiamslow
Helper III
Helper III

Sum by multiple criteria of the last transaction date

How do I create a measure sum "Qty" of each "Product", and "Block" of the last "TxnDate"? Please help thanks.

 

Data Table:

TxnDateQtySerialNProductBlock
1/1/236111AAABlockA
1/2/235111AAABlockA
1/3/233111AAABlockA
1/4/236111AAABlockA
1/5/235222AAABlockA
1/6/237222AAABlockA
1/7/233333BBBBlockA
1/8/236333BBBBlockA
1/9/237333BBBBlockA
1/10/234444BBBBlockA
1/11/237444BBBBlockA
1/1/234555AAABlockB
1/2/235555AAABlockB
1/3/236555AAABlockB
1/4/233555AAABlockB
1/5/235666AAABlockB
1/6/235666AAABlockB
1/7/238777BBBBlockB
1/8/234777BBBBlockB
1/9/236777BBBBlockB
1/10/238888BBBBlockB
1/11/234888BBBBlockB

 

Result: 2 measures

AAA_Qty = Sum of product AAA last TxnDate

BBB_Qty = Sum of product BBB last TxnDate

BlockAAA_QtyBBB_Qty
BlockA1314
BlockB810

 

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@EZiamslow You could use a matrix with Block as rows and Product as columns and this measure:

Measure = 
    VAR __Table = 
        ADDCOLUMNS(
            SUMMARIZE('Data', [SerialN], "__MaxTxnDate", MAX('Data'[TxnDate])),
            "__Qty", SUMX(FILTER( 'Data', [SerialN] = EARLIER([SerialN]) && [TxnDate] = [__MaxTxnDate]), [Qty])
        )
    VAR __Result = SUMX(__Table, [__Qty])
RETURN
    __Result

Or, if you really want two measures:

Measure AAA = 
    VAR __Product = "AAA"
    VAR __Table = 
        ADDCOLUMNS(
            SUMMARIZE(FILTER( 'Data', [Product] = __Product), [SerialN], "__MaxTxnDate", MAX('Data'[TxnDate])),
            "__Qty", SUMX(FILTER( 'Data', [SerialN] = EARLIER([SerialN]) && [TxnDate] = [__MaxTxnDate]), [Qty])
        )
    VAR __Result = SUMX(__Table, [__Qty])
RETURN
    __Result


Measure BBB = 
    VAR __Product = "BBB"
    VAR __Table = 
        ADDCOLUMNS(
            SUMMARIZE(FILTER( 'Data', [Product] = __Product), [SerialN], "__MaxTxnDate", MAX('Data'[TxnDate])),
            "__Qty", SUMX(FILTER( 'Data', [SerialN] = EARLIER([SerialN]) && [TxnDate] = [__MaxTxnDate]), [Qty])
        )
    VAR __Result = SUMX(__Table, [__Qty])
RETURN
    __Result


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

1 REPLY 1
Greg_Deckler
Super User
Super User

@EZiamslow You could use a matrix with Block as rows and Product as columns and this measure:

Measure = 
    VAR __Table = 
        ADDCOLUMNS(
            SUMMARIZE('Data', [SerialN], "__MaxTxnDate", MAX('Data'[TxnDate])),
            "__Qty", SUMX(FILTER( 'Data', [SerialN] = EARLIER([SerialN]) && [TxnDate] = [__MaxTxnDate]), [Qty])
        )
    VAR __Result = SUMX(__Table, [__Qty])
RETURN
    __Result

Or, if you really want two measures:

Measure AAA = 
    VAR __Product = "AAA"
    VAR __Table = 
        ADDCOLUMNS(
            SUMMARIZE(FILTER( 'Data', [Product] = __Product), [SerialN], "__MaxTxnDate", MAX('Data'[TxnDate])),
            "__Qty", SUMX(FILTER( 'Data', [SerialN] = EARLIER([SerialN]) && [TxnDate] = [__MaxTxnDate]), [Qty])
        )
    VAR __Result = SUMX(__Table, [__Qty])
RETURN
    __Result


Measure BBB = 
    VAR __Product = "BBB"
    VAR __Table = 
        ADDCOLUMNS(
            SUMMARIZE(FILTER( 'Data', [Product] = __Product), [SerialN], "__MaxTxnDate", MAX('Data'[TxnDate])),
            "__Qty", SUMX(FILTER( 'Data', [SerialN] = EARLIER([SerialN]) && [TxnDate] = [__MaxTxnDate]), [Qty])
        )
    VAR __Result = SUMX(__Table, [__Qty])
RETURN
    __Result


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

Check out the September 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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