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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
pmcinnis
Helper III
Helper III

Need DAX scripts that provide new table and new measures for a table visual

I'm hoping to get the DAX scripts that would build the table below in Power BI called 'Desired Output'. 'Desired Output' is produced from the data in the tables 'Results' and 'Guidelines'. I'm new to Power BI and DAX and seeing the scripts would help a lot in learning how the DAX functions work. If possible I'd like to get two solutions: one for a new table and one for a table visual. So the table visual scripts would be for new measures.

 

'Results' is a table of chemical concentration results. The column "result type" is the type of sample from which the concentrations are measured. 'Guidelines' is a table of health guidelines for the various chemicals. Concentrations should be below the guidelines for public safety. 'Desired Output' counts the number of results (for each result type for each chemical) that are equal to or over the guideline and equal to or over 1/2 the guideline.

 

Thanks in advance

 

Annotation 2020-01-06 115544.jpg

 

 

1 ACCEPTED SOLUTION
ChrisMendoza
Resident Rockstar
Resident Rockstar

@pmcinnis -

You should be able to accomplish this

20.png

in a Table Matrix Visual setup like

21.png

using these two measures:

# of results at or above 1/2 the guideline =
CALCULATE (
    COUNTROWS ( RESULTS ),
    FILTER (
        RESULTS,
        RESULTS[result]
            >= DIVIDE (
                RELATED ( GUIDELINES[guideline] ),
                2,
                -1
            )
    )
)
# of results at or above guideline =
CALCULATE (
    COUNTROWS ( RESULTS ),
    FILTER (
        RESULTS,
        RESULTS[result]
            >= RELATED ( GUIDELINES[guideline] )
    )
)





Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



View solution in original post

6 REPLIES 6
ChrisMendoza
Resident Rockstar
Resident Rockstar

@pmcinnis -

You should be able to accomplish this

20.png

in a Table Matrix Visual setup like

21.png

using these two measures:

# of results at or above 1/2 the guideline =
CALCULATE (
    COUNTROWS ( RESULTS ),
    FILTER (
        RESULTS,
        RESULTS[result]
            >= DIVIDE (
                RELATED ( GUIDELINES[guideline] ),
                2,
                -1
            )
    )
)
# of results at or above guideline =
CALCULATE (
    COUNTROWS ( RESULTS ),
    FILTER (
        RESULTS,
        RESULTS[result]
            >= RELATED ( GUIDELINES[guideline] )
    )
)





Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



Thanks Chris. This solution doesn't seem to provide separate counts for R, T and D result types. How do I do that?

@pmcinnis -

I don't understand what you mean.

4.png

Are you needing a Measure for some other type of Visual like a Card Visual? If I'm thinking correctly, that would mean you would have 6 Measures essentially all showing the same type of data just filtered differently.






Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



The DAX script doesn't seem to give separate counts for R, T and D result types. For example,  I think I would need three separate measures to get all three counts for results at or above guidelines. I think this works for the result type R count, is it correct?:

 

# of results at or above guideline for result type R =
CALCULATE(COUNTROWS(RESULTS),
FILTER(RESULTS,RESULTS[result]>=RELATED(GUIDELINES[guideline])),
FILTER(RESULTS,RESULTS[result type]="R"))
 

@pmcinnis -

Oh I see now. Yes I am letting the Visual handle the filtering for me. Your modification should work I would think. I don't have the sample data anymore and would have to recreate.

 

Yeah, your's works. I used:

# of R results at or above guideline = 
CALCULATE (
    COUNTROWS ( RESULTS ),
    RESULTS[result type] = "R",
    FILTER (
        RESULTS,
        RESULTS[result]
            >= RELATED ( guidelines[guideline] )
    )
)

I am unsure if one is better than the other, maybe a little easier to read?






Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



Apologies Chris, I hadn't notice the Visuals filtering part of your original solution (am still quite new to this). Your solution is more efficient than mine which creates 6 different measures.

 

Wondering if you could help me with some refinements to this solution. In my actual RESULTS table there are chemicals that don't have a corresponding guideline in the GUIDELINES table; however the solution is counting all the rows of results for those chemicals rather than returning a blank (or zero) result. For example, for chemical f with 7 rows of data but no guideline it will return a count of 7 rather than a blank (I need it to return a blank).  So I'm wondering:

(i) How do I tweak the solution DAX script so that it returns a blank for chemicals that don't have guidelines?

(ii) What is the DAX script to create a Yes/No measure (a new column) that indicates Yes or No ( or True or False) whether a chemical has a corresponding guideline?

 

Thanks in advance

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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