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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Anonymous
Not applicable

Count of Rows with and without condition in columns

I'm looking have a combined view of a count of all rows, and then a count of rows that meet a criteria. 

 

In this example I want to bucket by the "Stage" value. I will only need a count and sum of rev, but will need to filter and drill down - which is why a combined view is important. 

 

Source:

IDSub-IDStageRev
ADirect0$100
BIndirect0$125
CIndirect1$150
DDirect3$175
EDirect5$75
FIndirect6$100
GDirect6$50

 

Returned Matrix:

fdioebkenebiueipubheiupbhe.PNG

1 ACCEPTED SOLUTION
sturlaws
Resident Rockstar
Resident Rockstar

Hi@Anonymous 

 

I would first create a new table like this:

Stage bucket = DATATABLE("Bucket";STRING;{{"All"};{"1-5"};{"6"}})


Add 'Stage Bucket'[Bucket] to the columns of a matrix visualization.

Then create this measure

Number# = 
var sv=SELECTEDVALUE('Stage bucket'[Bucket])
return
SWITCH(
    true();
    sv="All";CALCULATE(COUNT('Table'[ID]);ALL('Table'[Stage]));
    sv="1-5";CALCULATE(COUNT('Table'[ID]);filter('Table';'Table'[Stage]<=5 && 'Table'[Stage]>0));
    sv="6";CALCULATE(COUNT('Table'[ID]);filter('Table';'Table'[Stage]=6));
    blank()
)

and this one

revenue = 
var sv=SELECTEDVALUE('Stage bucket'[Bucket])
return
SWITCH(
    true();
    sv="All";CALCULATE(sum('Table'[Rev]);ALL('Table'[Stage]));
    sv="1-5";CALCULATE(sum('Table'[Rev]);filter('Table';'Table'[Stage]<=5 && 'Table'[Stage]>0));
    sv="6";CALCULATE(sum('Table'[Rev]);filter('Table';'Table'[Stage]=6));
    blank()
)

 

Now add both these measures to the matrix visual as values. And then add Sub-ID and ID to the rows of the matrix visualization. This should give you a matrix looking like this:

sturlaws_1-1572392090376.png

 

Cheers,
Sturla


If this post helps, then please consider Accepting it as the solution. Kudos are nice too.

View solution in original post

8 REPLIES 8
sturlaws
Resident Rockstar
Resident Rockstar

Hi@Anonymous 

 

I would first create a new table like this:

Stage bucket = DATATABLE("Bucket";STRING;{{"All"};{"1-5"};{"6"}})


Add 'Stage Bucket'[Bucket] to the columns of a matrix visualization.

Then create this measure

Number# = 
var sv=SELECTEDVALUE('Stage bucket'[Bucket])
return
SWITCH(
    true();
    sv="All";CALCULATE(COUNT('Table'[ID]);ALL('Table'[Stage]));
    sv="1-5";CALCULATE(COUNT('Table'[ID]);filter('Table';'Table'[Stage]<=5 && 'Table'[Stage]>0));
    sv="6";CALCULATE(COUNT('Table'[ID]);filter('Table';'Table'[Stage]=6));
    blank()
)

and this one

revenue = 
var sv=SELECTEDVALUE('Stage bucket'[Bucket])
return
SWITCH(
    true();
    sv="All";CALCULATE(sum('Table'[Rev]);ALL('Table'[Stage]));
    sv="1-5";CALCULATE(sum('Table'[Rev]);filter('Table';'Table'[Stage]<=5 && 'Table'[Stage]>0));
    sv="6";CALCULATE(sum('Table'[Rev]);filter('Table';'Table'[Stage]=6));
    blank()
)

 

Now add both these measures to the matrix visual as values. And then add Sub-ID and ID to the rows of the matrix visualization. This should give you a matrix looking like this:

sturlaws_1-1572392090376.png

 

Cheers,
Sturla


If this post helps, then please consider Accepting it as the solution. Kudos are nice too.

Anonymous
Not applicable

Hey @sturlaws

 

I have another question on this.

I'm trying to filter this with a date slider using a date table. I have two measures [Date Min] and [Date Max] that I use in conjunction with DATESBETWEEN() to help me filter. 

The code looks as follows:

Number# = 
var sv=SELECTEDVALUE('Stage bucket'[Bucket])
return
SWITCH(
    true();
    sv="All",CALCULATE(COUNT('Table'[ID]),ALL('Table'[Stage]),DATESBETWEEN('Table'[Date1],[Date Min],[Date Max])),
    sv="1-5",CALCULATE(COUNT('Table'[ID]),filter('Table';'Table'[Stage]<=5 && 'Table'[Stage]>0),DATESBETWEEN('Table'[Date2],[Date Min],[Date Max])),
    sv="6",CALCULATE(COUNT('Table'[ID]),filter('Table';'Table'[Stage]=6)DATESBETWEEN('Table'[Date2],[Date Min],[Date Max])),
    blank()
)

 

Now, I get the right numbers returned for "All" and "6", but I am not able to get the right one for "1-5".

Without the date filter the values are correct, so the first part is working. I'm not sure why two of them are working, but the other is not. 

 

Any thoughts?

 

Yes 😃

 

First I would create two new tables, vDate and vStage. vDate you can create using the CALENDAR-function:

=CALENDAR ( DATE ( 2019, 1, 1 ), DATE ( 2019, 12, 31 ) )

just choose apropriate start and end dates.

 

For vStage you can use the 'Enter Data'-functionality in Power BI, you find it in the Home ribbon. Just enter the numbers 0,1,2,3,4,5,6 in the table, and name the column Stage.

Look at the schema view of your model. If the relationships are not created automatically, create relationship between vDate and Table on Date, and between vStage and Table on Stage.

 

then write this measure

Number# 2 =
VAR _sv =
    SELECTEDVALUE ( 'Stage bucket'[Bucket] )
VAR _minDate =
    MIN ( vDate[Date] )
VAR _maxDate =
    MAX ( vDate[Date] )
RETURN
    SWITCH (
        TRUE (),
        _sv = "All", CALCULATE (
            COUNT ( 'Table'[ID] ),
            ALL ( 'vStage' ),
            FILTER ( ALL ( vDate ), 'vDate'[Date] >= _minDate && vDate[Date] <= _maxDate )
        ),
        _sv = "1-5", CALCULATE (
            COUNT ( 'Table'[ID] ),
            FILTER ( ALL ( vStage ), 'vStage'[Stage] <= 5 && 'vStage'[Stage] > 0 ),
            FILTER ( ALL ( vDate ), 'vDate'[Date] >= _minDate && vDate[Date] <= _maxDate )
        ),
        _sv = "6", CALCULATE (
            COUNT ( 'Table'[ID] ),
            FILTER ( ALL ( vStage ), 'vStage'[Stage] = 6 ),
            FILTER ( ALL ( vDate ), 'vDate'[Date] >= _minDate && vDate[Date] <= _maxDate )
        ),
        BLANK ()
    )

 

Make sure you use date from vDate as value on your time slicer

 

Cheers,
Sturla

Anonymous
Not applicable

Thanks for the reply @sturlaws , I really appreciate your help.

 

I think this may solve one of my issues, but there is something I don't know if I made clear. 

 

For "All", "1-5" and "6" I'm actually filtering on different date columns in the same Table.

 

I know it's confusing, but for context this is analyzing pipeline, so imagine, "Created Date", "Active Pipeline Date" and "Close Date" 

confusing indeed.

Could you provide a sample dataset? And a mockup of your desired outcome?

Anonymous
Not applicable

Hey @sturlaws, the following is a mock dataset I put together. I've got a lot of exclusions and relationships that make it complicated but I think this will suffice. This example is only looking for the count, not the sum of rev like above. 

 

Source:

Date 1Date 2Stage
1/25/20192/14/20196
1/25/20192/21/20194
2/16/20194/13/20192
3/16/20194/10/20192
2/18/20193/7/20193
2/10/20192/24/20195
2/21/20194/18/20190
3/19/20194/11/20193
4/3/20196/7/20191
4/14/20195/10/20190
5/8/20195/20/20192
4/25/20195/23/20195
3/31/20196/6/20196
4/11/20194/26/20196
5/7/20195/11/20193
5/22/20195/23/20190
6/2/20196/10/20192
6/26/20197/16/20191
5/12/20195/28/20193
5/23/20196/16/20190
6/18/20197/21/20191
7/3/20197/31/20190
7/14/20197/16/20195
8/3/20198/28/20196
7/8/20197/25/20193
6/30/20197/14/20191
7/11/20199/5/20192
8/6/20198/29/20191
8/21/201910/25/20196
9/1/20199/27/20191
9/25/201910/7/20194
9/12/201910/10/20195
8/18/201910/24/20190
8/29/20199/13/20192
9/24/20199/28/20191
10/9/201910/10/20194
10/20/201910/28/20193
11/13/201912/3/20192
9/29/201910/15/20191
10/10/201911/3/20192
11/5/201912/8/20191
10/24/201911/8/20190
11/19/201911/23/20192
12/4/201912/5/20191
12/15/201912/23/20192

 

If filtering from between 3/1/2019 and 10/31/2019, expected result:

All"1-5""6"
34254

"All" is using Date 1 as only filter

"1-5" where stage is between 1-5 and using Date 2
"6" where stage is 6 using Date 2

Let me know if that doesn't make sense, and again thanks for the help.

I created a new table in the model with this sample data set, and called it NewData(not very imaginative, I know). Make sure the columns are of Date data type. Create connections to vDate[Date] with both 'NewData'[Date1] and 'NewData'[Date 2]. Also create the relationship with vStage.

 

Then create this measure:

Number# 3 =
VAR _sv =
    SELECTEDVALUE ( 'Stage bucket'[Bucket] )
VAR _minDate =
    MIN ( vDate[Date] )
VAR _maxDate =
    MAX ( vDate[Date] )
RETURN
    SWITCH (
        TRUE ();
        _sv = "All"; CALCULATE (
            COUNTROWS ( 'NewData' );
            ALL ( 'vStage' );
            FILTER ( ALL ( vDate ); 'vDate'[Date] >= _minDate && vDate[Date] <= _maxDate );
            USERELATIONSHIP ( NewData[Date 1]; vDate[Date] )
        );
        _sv = "1-5"; CALCULATE (
            COUNTROWS ( 'NewData' );
            FILTER ( ALL ( vStage ); 'vStage'[Stage] <= 5 && 'vStage'[Stage] > 0 );
            FILTER ( ALL ( vDate ); 'vDate'[Date] >= _minDate && vDate[Date] <= _maxDate );
            USERELATIONSHIP ( NewData[Date 2]; vDate[Date] )
        );
        _sv = "6"; CALCULATE (
            COUNTROWS ( 'NewData' );
            FILTER ( ALL ( vStage ); 'vStage'[Stage] = 6 );
            FILTER ( ALL ( vDate ); 'vDate'[Date] >= _minDate && vDate[Date] <= _maxDate );
            USERELATIONSHIP ( NewData[Date 1]; vDate[Date] )
        );
        BLANK ()
    )
Anonymous
Not applicable

Worked great! 

 

Thanks @sturlaws!

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.