Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rule Request] Default value missing from DIVIDE DAX function #60

Open
swolfe2 opened this issue Aug 8, 2024 · 0 comments
Open

[Rule Request] Default value missing from DIVIDE DAX function #60

swolfe2 opened this issue Aug 8, 2024 · 0 comments

Comments

@swolfe2
Copy link

swolfe2 commented Aug 8, 2024

Although [alternateresult] is an optional parameter within the DIVIDE function, it should be considered a best practice to provide a standard value in case the calculation was to fail.

I have a C# script to identify these today, but it would be great to be able to flag these within the BPA rules for Tabular Editor along with the ability to have a "fix script" to just supply BLANK() as the default value.
image

// Script to check if the DIVIDE function in measures has a default parameter set
string a = "";
foreach (var measure in Model.AllMeasures)
{
    var expression = measure.Expression;
    
    // Check if the measure's expression contains the DIVIDE function
    if (expression.Contains("DIVIDE"))
    {
        // Regex to match DIVIDE function with and without alternativeresult
        var divideRegex = new System.Text.RegularExpressions.Regex(@"DIVIDE\s*\(\s*[^,]+,\s*[^,]+(?:,\s*[^)]+)?\s*\)");
        var match = divideRegex.Match(expression);
        if (match.Success)
        {
            // Check if the DIVIDE function has three parameters
            var parameters = match.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (parameters.Length == 2)
            {
                // DIVIDE function without a default parameter
                a = a + measure.Name + ",";
            }
        }
    }
}

// Remove the trailing comma
if (a.EndsWith(","))
{
    a = a.Substring(0, a.Length - 1);
}

// Display the results
if (string.IsNullOrEmpty(a))
{
    Info("All DIVIDE functions have a default parameter.");
}
else
{
    Info("The following measures do not have a default parameter set for DIVIDE:\n" + a);
}

Default Value in DIVIDE DAX.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant