Scenario:
Suppose we have a Sample Dataset as follows:
Based on above Data, we need to calculate the Running Totals for Qnty_Sold by Prod_Segment
We can achieve this Scenario using the following M-Query:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hZJPawIxEMW/iuxZMH8mk+RYS60teClCD+Ih3Q0aus1KVks/frPLZkGD6+klkN+8zLzZ7Qq6YAtGGCnmhWAMIOrW1vbXta7x8fJS2/IcGu/Ktrv5Q218FU8oi/28w1XCOQeuoq5NqFz7PcEKOrCjNddKYNSP5nK2IUNXwfjSxgPgQOpEMkb0lOuIIgwoHVGKMIW++cqZqJoPJE8kgqQT300gkAGEBGpJuIz6fDSu41aX4N35EuzVfBTLOAqUR13a6oYa+6MJwjFPQqAz25qv2t7DeMpRJExx7LPIvVJfPFnJxAAQKe5YJUpmqTPEPrvc6d2cTLd9iNkk4pbhHadEgchGEY1U37arDt3TdfNjZ0+nU+26OVyvJ81WWwymn6Y9On+YbUwZ9WEdvK2jQWsWdeP+bJi9Buerfn3yKmM+Qt32IonqY31cJM1DxYb2/w==", BinaryEncoding.Base64), Compression.Deflate)),
let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Order_Date = _t, Order_Id = _t, Prod_Name = _t, Prod_Segment = _t, Country = _t, Qnty_Sold = _t]
),
ChangeType = Table.TransformColumnTypes(Source,{{"Order_Date", type date}, {"Order_Id", Int64.Type}, {"Prod_Name", type text}, {"Prod_Segment", type text}, {"Country", type text}, {"Qnty_Sold", Int64.Type}}),
Add_Index = Table.AddIndexColumn(ChangeType, "Index_No", 1, 1, Int64.Type),
/* Running Total Sales*/
Run_Sales = Table.AddColumn(Add_Index, "RT_Sales",
each List.Sum(
List.FirstN(
Add_Index[Qnty_Sold],
[Index_No]
)
)
),
/* Running Total Sales by Product Segment */
Seg_Run_Sales = Table.AddColumn(Run_Sales, "Segment_RT_Sales",
(OutTable) =>
List.Sum(
Table.SelectRows(Add_Index,
(InTable) => InTable[Index_No] <= OutTable[Index_No]
and
InTable[Prod_Segment] = OutTable[Prod_Segment]
)
[Qnty_Sold]
)
)
in
Seg_Run_Sales
Result:
Result:
No comments:
Post a Comment
Hi User, Thank You for visiting My Blog. If you wish, please share your genuine Feedback or comments only related to this Blog Posts. It is my humble request that, please do not post any Spam comments or Advertising kind of comments, which will be Ignored.