DAX to count based on a measure column?

情到浓时终转凉″ 提交于 2021-01-05 12:45:07

问题


I was kindly helped here: Power BI, DAX, Many-to-one and relational tables

to produce a measure column based on this data:

Builds = 
DATATABLE(
    "Build", STRING,
    "App", STRING,
    {
        { "Build1", "App1" },
        { "Build1", "AppNotInApps1" },
        { "Build1", "App2" },
        { "Build1", "App9" },
        { "Build2", "App3" },
        { "Build2", "AppNotInApps2" },
        { "Build3", "App1" },
        { "Build3", "App5" },
        { "Build3", "App8" },
        { "Build3", "App9" },
        { "Build3", "AppNotInApps3" }
    }
)
 
Apps = 
DATATABLE(
    "App", STRING,
    "Status", STRING,
    {
        { "App1", "UAT" },
        { "App2", "Complete" },
        { "App9", "New" },
        { "App3", "Complete" },
        { "App5", "UAT" },
        { "App8", "Complete" }
    }
)

Many Builds have one Apps, joined on Builds.App = Apps.App.

The measure column (called 'incomplete') was this little beauty:

incomplete = 
IF(
    HASONEVALUE( Builds[Build] ),
    VAR CurrentBuild =
        SELECTEDVALUE( Builds[Build] )
    VAR CurrentApp =
        SELECTEDVALUE( Apps[App] )
    VAR Result =
        COUNTROWS(
            FILTER(
                ALLNOBLANKROW( Builds ),
                Builds[Build] = CurrentBuild
                    && RELATED( Apps[Status] ) <> "Complete"
                    && NOT ISBLANK( RELATED( Apps[Status] ) )
            )
        ) + 0
    RETURN
        IF( NOT ISBLANK( SELECTEDVALUE( Apps[Status] ) ), Result )
)

Since this solution, I have also joined another table called Sites:

Sites = 
DATATABLE(
    "Site", STRING,
    "Build", STRING,
    {
        { "Site1", "Build1" },
        { "Site2", "Build1" },
        { "Site2", "Build3" }
    }
)

The join is on Sites.Build = Builds.Build. And you'll notice that one build can appear in more than one site (don't ask!).

I have a slicer on the Sites.Site column.

Here is some sample data:

Site.....Build.....App.....Status.....Incomplete
Site1....Build1....App1....UAT........2
Site1....Build1....App2....Complete...2
Site1....Build1....App9....New........2
Site2....Build1....App2....Complete...0
Site2....Build3....App1....UAT........3
Site2....Build3....App5....UAT........3
Site2....Build3....App8....Complete...1
Site2....Build3....App9....New........3

The 'Incomplete' gives me a count of how many Builds have a status that is not 'Complete'

What i want to do now (and this is the question) is have three cards in my BI view.

Card one (a measure?) - Count the DISTINCT number of Builds.Build that has an 'incomplete' count of 0. Based on the data above, this value would be 1. If i filtered this on Site1, this would be 0.

Card two (a measure?) - Count the DISTINCT number of Builds.Build that has an 'incomplete' count that is NOT equal to 0. Based on the data above, this value would be 2 (two distinct builds or Build1 and Build2). If i filtered this on Site2, this would be 1 (Build3).

Card three - card one measure as a percentage of (card one measure plus card 2 measure)

I have filters (slicers) on my page so i need the measure to be dynamic.

I've tried this for card 1, but it doesn't give me the correct value?

comp = CALCULATE(
DISTINCTCOUNT(Builds[Build]),
FILTER(ALLSELECTED(Build), [incomplete] = 0))

And of course this for card 2:

comp = CALCULATE(
    DISTINCTCOUNT(Builds[Build]),
    FILTER(ALLSELECTED(Build), [incomplete] <> 0))

Is there something I'm missing before i try my card 3?


回答1:


The following DAX worked:

COUNTROWS ( FILTER ( VALUES (Builds[build] ), Builds[Incomplete] = 0 ) )

I'm not entirely sure why this did NOT work:

COUNTROWS( DISTINCT( FILTER(Builds, Builds[Incomplete] = 0 ) ) ) 


来源:https://stackoverflow.com/questions/64774945/dax-to-count-based-on-a-measure-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!