Forum Discussion
barkha
9 months agoFrequent Visitor
Mix% (Dynamic date filters, NaturalInnerjoin, Summarize)
Input Sample Data: Customer SKU Date Rank Quantity A S100 Jan-24 1 200 A S100 Oct-24 2 400 A S200 Nov-24 3 45 A S100 Mar-25 4 100 A S100 Oct-25 5 200 A...
- 8 months ago
Hi Praful_Potphode ,
Please refer to the pbix below attatctched, and refer to the power query steps to get your desired output.
Hope this helps.
Thank you. - 8 months ago
barkha
Ok, I had the chance to implement the solution in the attached sample file.QAQB = () => VAR base_rank = SELECTEDVALUE ( BaseCalendar[Rank] ) VAR actual_rank = SELECTEDVALUE ( ActualCalendar[Rank] ) VAR timePeriod = SELECTEDVALUE ( TimePeriod[SelectedPeriod] ) - 1 // 1. VAR tbl1 = SUMMARIZE ( FILTER ( CleanSales, CleanSales[rank] >= base_rank - timePeriod && CleanSales[rank] <= base_rank ), All_CustomerIDs[CustomerNo], All_SKUCode[SKUCode], "QuantityB", SUM ( CleanSales[Quantity] ) ) // 2. VAR tbl2 = SUMMARIZE ( FILTER ( CleanSales, CleanSales[rank] >= actual_rank - timePeriod && CleanSales[rank] <= actual_rank ), All_CustomerIDs[CustomerNo], All_SKUCode[SKUCode], "QuantityA", SUM ( CleanSales[Quantity] ) ) // 3. VAR InnerJoinTable1 = NATURALINNERJOIN ( tbl1, tbl2 ) RETURN InnerJoinTable1MAMB = () => VAR base_rank = SELECTEDVALUE ( BaseCalendar[Rank] ) VAR actual_rank = SELECTEDVALUE ( ActualCalendar[Rank] ) VAR timePeriod = SELECTEDVALUE ( TimePeriod[SelectedPeriod] ) - 1 // 4. VAR tbl3 = CALCULATETABLE ( SUMMARIZE ( FILTER ( CleanSales, CleanSales[rank] >= base_rank - timePeriod && CleanSales[rank] <= base_rank ), All_CustomerIDs[CustomerNo], All_SKUCode[SKUCode], "QuantityB", SUM ( CleanSales[Quantity] ) ), ALL ( All_SKUCode[SKUCode] ) ) // 5. VAR tbl4 = CALCULATETABLE ( SUMMARIZE ( FILTER ( CleanSales, CleanSales[rank] >= actual_rank - timePeriod && CleanSales[rank] <= actual_rank ), All_CustomerIDs[CustomerNo], All_SKUCode[SKUCode], "QuantityA", SUM ( CleanSales[Quantity] ) ), ALL ( All_SKUCode[SKUCode] ) ) // 6. VAR InnerJoinTable2 = NATURALINNERJOIN ( tbl3, tbl4 ) // 7. VAR CustomerTotals = GROUPBY ( InnerJoinTable2, All_CustomerIDs[CustomerNo], "TotalBaseQuantity", SUMX ( CURRENTGROUP (), [QuantityB] ), "TotalActualQuantity", SUMX ( CURRENTGROUP (), [QuantityA] ) ) // 8. Merge totals back VAR FullWithTotals = NATURALLEFTOUTERJOIN ( QAQB(), CustomerTotals ) // 9. VAR MAMB = ADDCOLUMNS ( FullWithTotals, "Mn_A", DIVIDE ( [QuantityA], [TotalActualQuantity], 0 ), "Mn_B", DIVIDE ( [QuantityB], [TotalBaseQuantity], 0 ) ) RETURN MAMBCY Quantity = SUMX ( QAQB(), [QuantityB])CY Mix = AVERAGEX ( MAMB(), [Mn_B] )PY Quantity = SUMX ( QAQB(), [QuantityA] )PY Mix = AVERAGEX ( MAMB(), [Mn_A] )
Rufyda
8 months agoSuper User
You can calculate customer-level totals after the NATURALINNERJOIN using ADDCOLUMNS with SUMX(FILTER()) per customer, then merge back with the joined table. This ensures SKU-level Mix% divides correctly by the customer total. Using SUMMARIZECOLUMNS instead of SUMMARIZE can also simplify and optimize the calculation.
Try these solutions and let me know if you need further clarification
Regards,
Rufyda Rahma | MIE