Forum Discussion
Mix% (Dynamic date filters, NaturalInnerjoin, Summarize)
- 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] )
barkha Sorry, having trouble following, can you post sample data as text and expected output?
Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882
Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490
The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.