cancel
Showing results for 
Search instead for 
Did you mean: 

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

Reply
EZiamslow
Helper II
Helper II

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

@ 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!:
Mastering Power BI 2nd Edition

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

@ 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!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
PBI November 2023 Update Carousel

Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

Community News

Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

Power BI Fabric Summit Carousel

The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors