Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Hashmeet
Regular Visitor

Help Needed: Using Comparison Measures in Visuals (Card/Bar Charts) Without Aggregationfined

Hi Power BI Community,

I’m currently working on a dataset with two tables, Raw_Data 1 and Raw_Data 2, which contain the following columns:

  • Raw_Data 1: Need By Date 1 and Open Qty 1
  • Raw_Data 2: Need By Date 2 and Open Qty 2

For the purchase order and line number, the Need By Date and Open Qty values can either be the same or different between these tables.

Goal:

I’ve created measures to filter data based on user-selected dates and compare the Need By Date and Open Qty values from both tables. These comparison measures work fine in a table visual but I’m unable to use them effectively in other visuals, such as cards or bar charts.

Current Setup:

Measures for Filtering:

Raw_Data 1:

DAX
Need By Date First = SELECTEDVALUE('Raw_Data 1'[Need By Date 1])
Open Qty First = SELECTEDVALUE('Raw_Data 1'[Open Qty 1])

Raw_Data 2:

DAX
Need By Date Second =
CALCULATE
( SELECTEDVALUE('Raw_Data 2'[Need By Date 2]),
'Raw_Data 2'[Composite Key] = SELECTEDVALUE('Raw_Data 1'[Composite Key]) )

Open Qty Second = CALCULATE( SELECTEDVALUE('Raw_Data 2'[Open Qty 2]), 'Raw_Data 2'[Composite Key] = SELECTEDVALUE('Raw_Data 1'[Composite Key]) )

Measures for Comparison:

Date Comparison:

DAX
Date Comparison =
VAR d1 = [Need By Date First]
VAR d2 = [Need By Date Second]
RETURN
IF( ISBLANK(d1) && ISBLANK(d2), "No Date Present",
IF(NOT ISBLANK(d1) && ISBLANK(d2), "New Date Added",
IF(ISBLANK(d1) && NOT ISBLANK(d2), "Date Removed",
IF(d1 = d2, "Dates Match", "Dates Don't Match") ))) )

Qty Comparison:

DAX
Qty Comparison =
VAR d1 = [Open Qty First]
VAR d2 = [Open Qty Second]
RETURN
IF( ISBLANK(d1) && ISBLANK(d2), "No Qty Present",
IF(NOT ISBLANK(d1) && ISBLANK(d2), "New Qty Added",
IF(ISBLANK(d1) && NOT ISBLANK(d2), "Qty Removed",
IF(d1 = d2, "Qty Match", "Qty Don't Match") ))) )

Issue:

These measures work perfectly in a table visual, showing row-wise comparisons for Need By Date and Open Qty. However, I cannot use them in visuals like cards or bar charts since they rely on specific row context provided by the table visual.

I’ve considered creating calculated columns for comparison, but this approach requires using aggregation functions (e.g., MIN), which won’t work for scenarios where multiple dates exist for the same purchase order and line number.

Here’s an example of a calculated column I tried (but don’t want):

DAX
Calculated Date Comparison =
VAR d1 = 'Raw_Data 1'[Need By Date 1]
VAR d2 = CALCULATE( MIN('Raw_Data 2'[Need By Date 2]),
FILTER( 'Raw_Data 2', 'Raw_Data 2'[Composite Key] = 'Raw_Data 1'[Composite Key] ) )
RETURN
IF(ISBLANK(d1) && ISBLANK(d2), "No Date Present",
IF(NOT ISBLANK(d1) && ISBLANK(d2), "New Date Added",
IF(ISBLANK(d1) && NOT ISBLANK(d2), "Date Removed",
IF(d1 = d2, "Dates Match", "Dates Don't Match"))))

This is problematic because aggregating the dates (MIN, MAX) loses the granularity I need.

Question:

How can I make my comparison measures (Date Comparison and Qty Comparison) usable in visuals like cards and bar charts? Is there a way to summarize or aggregate these measures meaningfully while maintaining their row-level logic?

Any suggestions or best practices would be greatly appreciated!

Thank you!

1 ACCEPTED SOLUTION
DataNinja777
Super User
Super User

Hi @Hashmeet ,

 

To solve the issue of using your comparison measures in visuals like cards and bar charts, we need to adjust your DAX formulas so that they can aggregate meaningful results across the entire dataset. The current setup works well in a table visual because it relies on row context to evaluate the differences between Raw_Data 1 and Raw_Data 2. However, when you want to use these measures in summary visuals, you need to aggregate the comparison outcomes meaningfully without losing granularity.

The key is to convert your comparison results into numeric values that can be counted or aggregated, rather than returning text outputs like "Dates Match" or "Qty Match." By doing this, you can create summary measures that count the number of matches, mismatches, or changes across the entire dataset.

First, modify your Date Comparison and Qty Comparison measures to return numeric values. For Date Comparison, the measure can be rewritten as follows:

Date Comparison Numeric =
VAR d1 = [Need By Date First]
VAR d2 = [Need By Date Second]
RETURN
SWITCH(
    TRUE(),
    ISBLANK(d1) && ISBLANK(d2), 0,         // No Date Present
    NOT ISBLANK(d1) && ISBLANK(d2), 1,     // New Date Added
    ISBLANK(d1) && NOT ISBLANK(d2), 2,     // Date Removed
    d1 = d2, 3,                            // Dates Match
    4                                      // Dates Don't Match
)

Similarly, modify the Qty Comparison measure to return numeric values:

Qty Comparison Numeric =
VAR q1 = [Open Qty First]
VAR q2 = [Open Qty Second]
RETURN
SWITCH(
    TRUE(),
    ISBLANK(q1) && ISBLANK(q2), 0,         // No Qty Present
    NOT ISBLANK(q1) && ISBLANK(q2), 1,     // New Qty Added
    ISBLANK(q1) && NOT ISBLANK(q2), 2,     // Qty Removed
    q1 = q2, 3,                            // Qty Match
    4                                      // Qty Don't Match
)

Once these numeric measures are created, you can aggregate them across the dataset to show meaningful counts in visuals. For example, you can create a measure to count how many times the dates match between the two tables:

Count of Dates Match =
CALCULATE(
    COUNTROWS('Raw_Data 1'),
    [Date Comparison Numeric] = 3
)

Similarly, create a measure to count how many times the quantities match:

Count of Qty Match =
CALCULATE(
    COUNTROWS('Raw_Data 1'),
    [Qty Comparison Numeric] = 3
)

You can also create measures to count other outcomes, such as new dates added or dates removed. The aggregation logic can be extended further by calculating percentages. For example, to calculate the percentage of matching dates:

Date Match Percentage = 
DIVIDE(
    [Count of Dates Match],
    COUNTROWS('Raw_Data 1'),
    0
)

If you want to visualize the different comparison outcomes in a bar chart, create a measure that returns the category label based on the numeric value from your comparison measure. For example, to categorize the date comparison results:

Date Comparison Categories =
SWITCH(
    TRUE(),
    [Date Comparison Numeric] = 1, "New Date Added",
    [Date Comparison Numeric] = 2, "Date Removed",
    [Date Comparison Numeric] = 3, "Dates Match",
    [Date Comparison Numeric] = 4, "Dates Don't Match",
    "No Date Present"
)

This measure can be used as a legend or axis in a bar chart to display the distribution of comparison outcomes across the dataset.

By using this approach, you retain the row-level logic in your comparisons while enabling meaningful aggregation for summary visuals. You can count the occurrences of each comparison outcome and visualize them effectively in cards, bar charts, and other visuals. This ensures that your comparison logic is not lost when you switch from a table visual to summary visuals, providing a more comprehensive view of your data comparisons.

 

Best regards,

View solution in original post

2 REPLIES 2
v-xianjtan-msft
Community Support
Community Support

Hi @Hashmeet 

 

Thank you DataNinja777 for your wonderful reply.
May I ask if DataNinja777's reply helped you solve your problem? If so please consider accepting his reply as a solution, it will make it easier for other users with similar problems to find a solution.

 

Best Regards,
Jarvis Tang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

DataNinja777
Super User
Super User

Hi @Hashmeet ,

 

To solve the issue of using your comparison measures in visuals like cards and bar charts, we need to adjust your DAX formulas so that they can aggregate meaningful results across the entire dataset. The current setup works well in a table visual because it relies on row context to evaluate the differences between Raw_Data 1 and Raw_Data 2. However, when you want to use these measures in summary visuals, you need to aggregate the comparison outcomes meaningfully without losing granularity.

The key is to convert your comparison results into numeric values that can be counted or aggregated, rather than returning text outputs like "Dates Match" or "Qty Match." By doing this, you can create summary measures that count the number of matches, mismatches, or changes across the entire dataset.

First, modify your Date Comparison and Qty Comparison measures to return numeric values. For Date Comparison, the measure can be rewritten as follows:

Date Comparison Numeric =
VAR d1 = [Need By Date First]
VAR d2 = [Need By Date Second]
RETURN
SWITCH(
    TRUE(),
    ISBLANK(d1) && ISBLANK(d2), 0,         // No Date Present
    NOT ISBLANK(d1) && ISBLANK(d2), 1,     // New Date Added
    ISBLANK(d1) && NOT ISBLANK(d2), 2,     // Date Removed
    d1 = d2, 3,                            // Dates Match
    4                                      // Dates Don't Match
)

Similarly, modify the Qty Comparison measure to return numeric values:

Qty Comparison Numeric =
VAR q1 = [Open Qty First]
VAR q2 = [Open Qty Second]
RETURN
SWITCH(
    TRUE(),
    ISBLANK(q1) && ISBLANK(q2), 0,         // No Qty Present
    NOT ISBLANK(q1) && ISBLANK(q2), 1,     // New Qty Added
    ISBLANK(q1) && NOT ISBLANK(q2), 2,     // Qty Removed
    q1 = q2, 3,                            // Qty Match
    4                                      // Qty Don't Match
)

Once these numeric measures are created, you can aggregate them across the dataset to show meaningful counts in visuals. For example, you can create a measure to count how many times the dates match between the two tables:

Count of Dates Match =
CALCULATE(
    COUNTROWS('Raw_Data 1'),
    [Date Comparison Numeric] = 3
)

Similarly, create a measure to count how many times the quantities match:

Count of Qty Match =
CALCULATE(
    COUNTROWS('Raw_Data 1'),
    [Qty Comparison Numeric] = 3
)

You can also create measures to count other outcomes, such as new dates added or dates removed. The aggregation logic can be extended further by calculating percentages. For example, to calculate the percentage of matching dates:

Date Match Percentage = 
DIVIDE(
    [Count of Dates Match],
    COUNTROWS('Raw_Data 1'),
    0
)

If you want to visualize the different comparison outcomes in a bar chart, create a measure that returns the category label based on the numeric value from your comparison measure. For example, to categorize the date comparison results:

Date Comparison Categories =
SWITCH(
    TRUE(),
    [Date Comparison Numeric] = 1, "New Date Added",
    [Date Comparison Numeric] = 2, "Date Removed",
    [Date Comparison Numeric] = 3, "Dates Match",
    [Date Comparison Numeric] = 4, "Dates Don't Match",
    "No Date Present"
)

This measure can be used as a legend or axis in a bar chart to display the distribution of comparison outcomes across the dataset.

By using this approach, you retain the row-level logic in your comparisons while enabling meaningful aggregation for summary visuals. You can count the occurrences of each comparison outcome and visualize them effectively in cards, bar charts, and other visuals. This ensures that your comparison logic is not lost when you switch from a table visual to summary visuals, providing a more comprehensive view of your data comparisons.

 

Best regards,

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.