0% found this document useful (0 votes)
3 views

Calculation Group Where SELECTEDMEASURE

The document outlines a solution for dynamically applying filters to Sales and Inventory metrics in Power BI using a calculation group. It includes details on creating slicers, capturing selected metrics, and implementing DAX code to filter data based on user selections. The expected behavior ensures that the correct filters are applied for both single and multi-select scenarios, enhancing data analysis capabilities.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Calculation Group Where SELECTEDMEASURE

The document outlines a solution for dynamically applying filters to Sales and Inventory metrics in Power BI using a calculation group. It includes details on creating slicers, capturing selected metrics, and implementing DAX code to filter data based on user selections. The expected behavior ensures that the correct filters are applied for both single and multi-select scenarios, enhancing data analysis capabilities.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

calculation group where SELECTEDMEASURE() dynamically applies different filters based on

the selected Sales or Inventory metrics, while also handling multi-select options from both
slicers.

🛠 Solution Overview

1️⃣ Slicers:

 Sales Metrics Slicer: Total Sales $, Total Sales Cost $, Total Sales Unit

 Inventory Metrics Slicer: Total RTV Rtl $, Total RTV Cost, Total RTV Units

2️⃣ Calculation Group Items (TY, LY, LLY):

 TY (This Year): Filters data for the current year.

 LY (Last Year): Filters data for the previous year.

 LLY (Two Years Ago): Filters data for two years ago.

📌 CDAX for Calculation Group (TY, LY, LLY)

In Tabular Editor, modify your calculation group as follows:

Dynamic Time Calculation =

VAR _SelectedSalesMetrics =

CONCATENATEX(VALUES('Sales Metrics Table'[Metric]), 'Sales Metrics Table'[Metric], ", ")

VAR _SelectedInventoryMetrics =

CONCATENATEX(VALUES('Inventory Metrics Table'[Metric]), 'Inventory Metrics


Table'[Metric], ", ")

VAR _IsSalesMetricSelected = NOT(ISBLANK(_SelectedSalesMetrics))

VAR _IsInventoryMetricSelected = NOT(ISBLANK(_SelectedInventoryMetrics))

VAR _DateFilter =

SWITCH(

SELECTEDVALUE('Calculation Group'[Calculation Type]),


"TY", DATESYTD('Date'[Date]),

"LY", SAMEPERIODLASTYEAR(DATESYTD('Date'[Date])),

"LLY", CALCULATE(DATESYTD('Date'[Date]), DATEADD('Date'[Date], -2, YEAR)),

BLANK()

VAR _FilteredMeasure =

SWITCH(

TRUE(),

_IsSalesMetricSelected, CALCULATE(SELECTEDMEASURE(), _DateFilter, 'Sales


Data'[Metric] IN VALUES('Sales Metrics Table'[Metric])),

_IsInventoryMetricSelected, CALCULATE(SELECTEDMEASURE(), _DateFilter, 'Inventory


Data'[Metric] IN VALUES('Inventory Metrics Table'[Metric])),

SELECTEDMEASURE()

RETURN

_FilteredMeasure

🔹 How This Works

1. Captures the selected values from both the Sales and Inventory slicers using
VALUES().

2. Checks which slicer has a selection (_IsSalesMetricSelected and


_IsInventoryMetricSelected).

3. Applies the correct time-based filter (TY, LY, LLY) based on the calculation group.

4. Filters data based on whether the selected measure belongs to Sales or Inventory.

5. Handles multi-selection from both slicers using IN VALUES().

🔹 Expected Behavior
 If the user selects "Total Sales $", it applies the TY/LY/LLY filter to Sales Data.

 If the user selects "Total RTV Cost", it applies the TY/LY/LLY filter to Inventory Data.

 If the user selects both "Total Sales $" and "Total RTV Cost", both are filtered
accordingly.

🚀 Next Steps

1. Create a disconnected table for Sales Metrics Table and Inventory Metrics Table to
store available metrics.

2. Add slicers for both tables in your Power BI report.

3. Assign the Calculation Group to your visuals.

Would you like additional modifications, such as handling ALL selections or default
behaviors? 😊

🚀 Step-by-Step Implementation in Power BI

Here’s the complete DAX code for setting up the required tables, slicers, and measures in
Power BI.

🟢 Step 1: Create Sales Metrics Table (Disconnected)

This table contains all Sales metrics for the slicer.

SalesMetricsTable =

DATATABLE(

"Metric", STRING,

{"Total Sales $"},

{"Total Sales Cost $"},

{"Total Sales Unit"}

)
🟢 Step 2: Create Inventory Metrics Table (Disconnected)

This table contains all Inventory metrics for the slicer.

InventoryMetricsTable =

DATATABLE(

"Metric", STRING,

{"Total RTV Rtl $"},

{"Total RTV Cost"},

{"Total RTV Units"}

🟢 Step 3: Create Measure to Capture Selected Sales Metrics

This measure stores multi-selected values from the Sales Metrics slicer.

SelectedSalesMetrics =

VAR _Selected = VALUES(SalesMetricsTable[Metric])

RETURN

IF(COUNTROWS(_Selected) = COUNTROWS(ALL(SalesMetricsTable[Metric])),

"All",

CONCATENATEX(_Selected, SalesMetricsTable[Metric], ", ")

🔹 If all metrics are selected, it returns "All"; otherwise, it concatenates the selected metrics.

🟢 Step 4: Create Measure to Capture Selected Inventory Metrics

Same as the Sales measure but for Inventory.

SelectedInventoryMetrics =

VAR _Selected = VALUES(InventoryMetricsTable[Metric])

RETURN
IF(COUNTROWS(_Selected) = COUNTROWS(ALL(InventoryMetricsTable[Metric])),

"All",

CONCATENATEX(_Selected, InventoryMetricsTable[Metric], ", ")

🟢 Step 5: Create Calculation Group in Tabular Editor

1. Open Tabular Editor.

2. Create a Calculation Group named "Time Calculations".

3. Add the following calculation items:

🔹 Calculation Item: Dynamic Time Calculation

Dynamic Time Calculation =

VAR _SelectedSalesMetrics =

VALUES(SalesMetricsTable[Metric])

VAR _SelectedInventoryMetrics =

VALUES(InventoryMetricsTable[Metric])

VAR _DateFilter =

SWITCH(

SELECTEDVALUE('Time Calculations'[Calculation Type]),

"TY", DATESYTD('Date'[Date]),

"LY", SAMEPERIODLASTYEAR(DATESYTD('Date'[Date])),

"LLY", CALCULATE(DATESYTD('Date'[Date]), DATEADD('Date'[Date], -2, YEAR)),

BLANK()

VAR _FilteredMeasure =

SWITCH(
TRUE(),

NOT ISEMPTY(_SelectedSalesMetrics),

CALCULATE(SELECTEDMEASURE(), _DateFilter, 'Sales Data'[Metric] IN


_SelectedSalesMetrics),

NOT ISEMPTY(_SelectedInventoryMetrics),

CALCULATE(SELECTEDMEASURE(), _DateFilter, 'Inventory Data'[Metric] IN


_SelectedInventoryMetrics),

SELECTEDMEASURE()

RETURN

_FilteredMeasure

🟢 Step 6: Add Slicers & Apply the Calculation Group

1. Add slicers for:

o SalesMetricsTable[Metric]

o InventoryMetricsTable[Metric]

o Time Calculations[Calculation Type] (for TY, LY, LLY)

2. Use a visual (like a table or chart) and add measures (e.g., Total Sales $, Total RTV
Cost).

3. Apply the Calculation Group to your measure in the visual.

🎯 Expected Behavior

 If "Total Sales $" is selected, the calculation applies to Sales Data.

 If "Total RTV Cost" is selected, the calculation applies to Inventory Data.

 If multiple metrics are selected, the correct filters apply dynamically.

 If all metrics are selected, everything is displayed.


🔥 Final Thoughts

✅ Handles multi-selection in slicers.


✅ Filters Sales & Inventory separately within a single calculation group.
✅ Applies TY, LY, LLY dynamically to selected metrics.
✅ Uses disconnected tables to make it work efficiently.

Let me know if you need refinements or additional logic! 🚀😊

You might also like