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
MRIZ
Helper II
Helper II

Inserting New Column in Table SUM (UnitPrice) Group By (ReceiptNo) in Transform Data (Power Query)

Dear Members, 

I want to add a new column in a factsales table in Power Query which will summerise UnitPrice group by ReceiptNo. 
Below is the actual image:

MRIZ_1-1713264516975.png

 

What I want is new column as shown below:

MRIZ_2-1713264729632.png

 

Thanks. 
Rizwan.

4 REPLIES 4
dufoq3
Super User
Super User

Hi @MRIZ, try this:

 

dufoq3_0-1713266414049.png

 

Result

dufoq3_1-1713266430032.png

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Hi, Thanks for the response and yes it works. What if I want to check some condition like SUM UnitPrice only where RowisActive = 1 Group By ReceiptNo. 

Do you mean something like this?

I've added such filter in GroupedRows step.

 

Result

dufoq3_0-1713267813472.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLSszQHUoZKsToQARM9IxMgZQAWMAKrMDSCqzAGsfTMLOECJggVEC2mQJapnqUxXIUpwhagilgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ReceiptNo = _t, UnitPrice = _t, RowisActive = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"ReceiptNo", Int64.Type}, {"UnitPrice", type number}, {"RowisActive", Int64.Type}}, "en-US"),
    GroupedRows = Table.Group(ChangedType, {"ReceiptNo"}, {
        {"All", each _, type table [ReceiptNo=nullable number, UnitPrice=nullable number, RowisActive=nullable number]},
        {"SumUnitPrice", each List.Sum([UnitPrice]), type number},
        {"SumUnitPrice RowisActive 1", each List.Sum(Table.SelectRows(_, (x)=> x[RowisActive] = 1)[UnitPrice]), type number}
    }),
    Expanded = Table.ExpandTableColumn(GroupedRows, "All", {"UnitPrice", "RowisActive"}, {"UnitPrice", "RowisActive"})
in
    Expanded

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

So far I followed your solution and its working, but my requirement is more complex. This is what i want to achieve. See this SQL 

CASE WHEN  (IsRefundSaleOrder = 1 AND IsRefund = 0 )  THEN 0
		 WHEN  (IsRefundSaleOrder = 1 AND IsRefund = 1 )  THEN (SELECT SUM(DISTINCT (ROUND([UnitPrice],2)))  
																FROM dw.Fact_Sales a 
																WHERE a.SaleOrderID = sal_ord.SaleOrderId 
																--AND a.SaleOrderItemID = sal_ord.SaleOrderItemID 
																AND a.RowIsActive = 1 AND a.IsRefundSaleOrder = 1 AND a.IsRefund = 1
																GROUP BY a.SaleOrderId)
		  ELSE (SELECT SUM(DISTINCT (ROUND([UnitPrice],2)))  FROM dw.Fact_Sales a 
				 WHERE a.SaleOrderID = sal_ord.SaleOrderId 
				 --AND a.SaleOrderItemID = sal_ord.SaleOrderItemID 
				 AND a.RowIsActive = 1 
				 GROUP BY a.SaleOrderId) 
	END AS SumOfUnitPrices

If you can help in this as this is me first ever M Query. 

thanks.

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.