Code Generator | Power BI

DAX Fix for Rule Calculation

This document outlines modifications to the DAX formula for Rule #16, incorporating ValuationDate into DISTINCTCOUNT, ensuring accurate unique counts of CntTagUniqueName. It includes the original and updated code along with key changes


Empty image or helper icon

Prompt

How can I fix this DAX? -  Rule #16 = 
VAR _DisCountR16 =
CALCULATE(
    DISTINCTCOUNT('Rule16_EYR_CheckYieldRateValues'[CntTagUniqueName], 'Rule16_EYR_CheckYieldRateValues'[VALUATIDATE] ),
    'Rule16_EYR_CheckYieldRateValues'[YieldRateRule] IN { 1 }
)
Return
IF(ISBLANK(_DisCountR16),"0",_DisCountR16) - How would I add a field clled ValuationDate from same table to the DISTINCTCOUNT section?

Answer

Fixing the DAX for Rule #16

Overview

The objective is to modify the existing DAX formula for Rule #16 by adding the ValuationDate field to the DISTINCTCOUNT function.

Original DAX Code

Rule #16 = 
VAR _DisCountR16 =
CALCULATE(
    DISTINCTCOUNT('Rule16_EYR_CheckYieldRateValues'[CntTagUniqueName], 'Rule16_EYR_CheckYieldRateValues'[VALUATIDATE]),
    'Rule16_EYR_CheckYieldRateValues'[YieldRateRule] IN { 1 }
)
RETURN
IF(ISBLANK(_DisCountR16), "0", _DisCountR16)

Issues Observed

  1. DISTINCTCOUNT Syntax: The way DISTINCTCOUNT is used is incorrect. It should count only one column, not multiple columns.
  2. Additional Field Requirement: We need to incorporate the ValuationDate into the distinct count calculation.

Proposed Solution

To calculate the distinct count while also considering the ValuationDate, we can concatenate CntTagUniqueName and ValuationDate, which allows us to create a unique identifier for each combination.

Updated DAX Code

Here’s the refined code that addresses the above issues:

Rule #16 = 
VAR _DisCountR16 =
CALCULATE(
    DISTINCTCOUNT(
        'Rule16_EYR_CheckYieldRateValues'[CntTagUniqueName & ' - ' & 'Rule16_EYR_CheckYieldRateValues'[ValuationDate]]
    ),
    'Rule16_EYR_CheckYieldRateValues'[YieldRateRule] IN { 1 }
)
RETURN
IF(ISBLANK(_DisCountR16), "0", _DisCountR16)

Key Changes Explained

  • Concatenation: A new unique identifier is created by combining CntTagUniqueName and ValuationDate. This is necessary as DISTINCTCOUNT can only operate on a single column at a time.
  • Correct Function Usage: The DISTINCTCOUNT is now correctly counting the unique combinations.

Conclusion

This modified DAX formula efficiently calculates the distinct count of the unique tags per ValuationDate whenever YieldRateRule equals 1, while also handling blank values gracefully.

Next Steps

Test the modified DAX code in your Power BI environment to ensure it captures the intended results. For enhanced DAX knowledge, exploring courses on the Enterprise DNA Platform can be beneficial.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This document outlines modifications to the DAX formula for Rule #16, incorporating ValuationDate into DISTINCTCOUNT, ensuring accurate unique counts of CntTagUniqueName. It includes the original and updated code along with key changes for clarity.