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
mblom
Frequent Visitor

Get Sum of quantity in related table based on Max of date

Hi!

 

I am new to power bi and I am stuck with the following requirement:

 

I have 3 tables:

Product: productid, description

Purchase: purchaseid, purchasedate

PurchaseProduct: purchaseid, productid, quantity

 

In my table visual I would like to list all products with their id and description. Furthermore, I would like to show the SUM of Quantity (SUM of PurchaseProduct.Quantity) for the most recent Purchasedate (MAX of purchasedate).

 

I have been trying to summarize these values with M while getting the data from the sources but this failed. I also feel that I should be able to get this calculation using DAX.

 

Sample data for Product:

ProductIdDescription
1Tape
2Mainboard
3LCD
4Screw
5Backcover

 

Sample data for Purchase:

PurchaseIdPurchasedate
12021-01-10
22021-01-12
32021-01-14
42021-02-01

 

Sample data for PurchaseProduct:

PurchaseidProductidQuantity
115
122
146
2110
2220
335
347
4110
458
4120

 

The expected result would be a table like:

ProductidDescriptionLast purchasedateLast total quantity
1Tape2021-02-0130
2Mainboard2021-01-1220
3LCD2021-01-145
4Screw2021-01-147
5Backcover2021-02-018

 

Any help would be appreciated!

 

Mark

1 ACCEPTED SOLUTION
mahoneypat
Employee
Employee

I assume your model has 1:M relationships on Product ID and Purchase ID (and that your PurchaseDate column is type Date).  You should consider adding a Date table to your model.  You can use a measure expression like this in your table visual with Product ID and Description.

 

Latest Purchase Amt =
LASTNONBLANKVALUE (
    Purchase[PurchaseDate],
    CALCULATE (
        SUM ( PurchaseProduct[Quantity] )
    )
)

 

If interested, hear is an article/video on adding a Date table.

Creating a simple date table in DAX - SQLBI

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

4 REPLIES 4
mahoneypat
Employee
Employee

I assume your model has 1:M relationships on Product ID and Purchase ID (and that your PurchaseDate column is type Date).  You should consider adding a Date table to your model.  You can use a measure expression like this in your table visual with Product ID and Description.

 

Latest Purchase Amt =
LASTNONBLANKVALUE (
    Purchase[PurchaseDate],
    CALCULATE (
        SUM ( PurchaseProduct[Quantity] )
    )
)

 

If interested, hear is an article/video on adding a Date table.

Creating a simple date table in DAX - SQLBI

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Thanks, that works like charm!

timg
Solution Sage
Solution Sage

Hi mblom,

you could use this method.

Step 1 is making a calculated column in the Purchaseproduct table with the related Purchase date for eacht order

PurchaseDate = RELATED(Purchase[Purchasedate])

 Step 2 is making a measure in which only the quantity related to the max date is summed up:

MaxProductdateQuantity = 
CALCULATE (
    SUM ( PurchaseProduct[Quantity] ),
    FILTER (
        PurchaseProduct,
        PurchaseProduct[PurchaseDate] = MAX ( PurchaseProduct[PurchaseDate] )
    )
)

Lastly, you can place the calculated column and measure in a table to show the desired result (set the calculated column to "latest" to show the last date per productid).

1.PNG

Hope that helps!

 

Regards,

Tim





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




mblom
Frequent Visitor

Looks like a viable solution as well! I will try this tomorrow. Thanks for your suggestion!

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.