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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
sevens0001
Frequent Visitor

Countifs in Power BI

Hi, everyone! 

 

I'm trying to convert a countifs formula from an excel report into Power BI. It has two date context filters and I'm having a hard time wrapping my head around the logic and syntax. 

 

The problem: 

In my data I have job records that have an open date, a cutoff date, and a calculation that counts the difference between the two (days open). I need to be able to count how many jobs were actively open over time as well as the median for the days open metric. 

 

My model:

- I have a calendar table that controls a universal date slicer 

- I have inactive relationships from both Open Date and Cufoff Date to my my calendar date

 

My Current Metric: 

Open Reqs by Time = CALCULATE([Open Reqs], <- this is basically a row count
FILTER(Jobs, Jobs[Open Date] <= MAX('Calendar'[Date])), 
FILTER(Jobs, Jobs[Cutoff Date] >= MAX('Calendar'[Date])))
 
Median Days Open = CALCULATE(MEDIAN(Jobs[Days Open]),
FILTER(Jobs, Jobs[Open Date] <= MAX('Calendar'[Date])),
FILTER(Jobs, Jobs[Cutoff Date] >= MAX('Calendar'[Date])))
 
I'm modeling this grouped by month/year. To look at this historically, I can only count a job as being open IF its open date is less than the month/year AND it's Cuffoff date is greater than the same month/year. 
 
I'm not convinced that the metrics that I've written are using the filters like a countifs would in Excel. 
 
Thanks for any help! 
12 REPLIES 12
Ashish_Mathur
Super User
Super User

Hi,

Why do you have 2 inactive relationships - only one should be inactive?  Share some data and show the expected result.  


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

hi, Ashish! 

 

I have the inactive relationships because I have other metrics that are based off of those dates as well (not as complicated as this), and I want everything tied to one date slicer. Here's some sample data and the desired output. 

The data: 

sevens0001_0-1630332045601.png

 

The Outpout: 

sevens0001_1-1630332073262.png

 

Hi,

Share data in a form that can be pasted in an MS Excel file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Ashish, I'm not sure the best way to go about that, but here's some sample data below. Let me know if this doesn't work. Thanks! 

Job IDOpen DateCutoff DateDays Open
1001/1/2021 242
1052/26/20215/7/202170
1073/1/20214/13/202143
1263/22/2021 162
1273/22/20216/14/202184
2094/7/20214/26/202119
2104/7/20215/3/202126
2114/7/2021 146
2915/4/20218/26/2021114
2925/4/2021 119
2935/4/20218/10/202198
2945/4/2021 119
2955/5/2021 118
2965/5/20218/24/2021111
2975/5/20217/1/202157
3335/10/20218/2/202184
3345/11/2021 112
3355/11/2021 112
3807/2/2021 60
3817/6/2021 56
4978/6/20218/17/202111
4988/6/2021 25
4998/7/20218/10/20213
5008/9/2021 22
5018/9/2021 22
5028/9/2021 22

Hi,

You may download my solution file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thank you, Ashish - 

 

I was able to follow how you transformed the data. Unfortunantly I can't expand my rows by the months that it's open. Too many other metrics rely on distinct agreggations on other fields in 'Jobs' table. It would also degrade the performance. I have over 4000 rows of data already with 5 other sources pulling in. Thank you though! 

@sevens0001 

you can try this

Open Reqs = CALCULATE(COUNTROWS('Table'),FILTER('Table',('Table'[Open Date]<=max('date'[Date])&&ISBLANK('Table'[cutoff date]))||'Table'[Open Date]<=max('date'[Date])&&'Table'[cutoff date]>max('date'[Date])))

median = CALCULATE(AVERAGE('Table'[Days Open]),FILTER('Table',('Table'[Open Date]<=max('date'[Date])&&ISBLANK('Table'[cutoff date]))||'Table'[Open Date]<=max('date'[Date])&&'Table'[cutoff date]>max('date'[Date])))

I am not sure why the median for july and august is different from your expected output.

pls see the attachment below





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




This actually produced the same result as my original metric! I feel like that is a good sign, but I'm going to wait and hold off on marking it as a solution until I test something else. Thank you! 

daxer-almighty
Solution Sage
Solution Sage

The other one must be adjusted accordingly. Of course, for this to work Calendar must not be connected to Open Date and Cutoff Date.

 

Open Reqs =
var MinDate = MIN( 'Calendar'[Date] )
var MaxDate = MAX( 'Calendar'[Date] )
var Result =
    CALCULATE(
        COUNTROWS( Jobs ),
        // If the Open Date field is going to be
        // hidden from the user so that they can't
        // slice by it, then you can remove
        // KEEPFILTERS and only leave the condition
        // underneath. Same goes for the other filter.
        KEEPFILTERS( Jobs[Open Date] <= MinDate ),
        KEEPFILTERS( MaxDate <= Jobs[Cutoff Date] )
    )
return
    Result

 

Thanks, Daxer! 

 

I do need my table connected to the calendar table because there are other metrics being calculated that require me to use "userelationship". Everything in the end should be controlled by a date slicer. 

To disconnect the table in my measure just use CROSSFILTER with the NONE direction. Easy as 1-2-3.

Hi, Daxer! 

 

I apologize, I'm not sure what you mean? Are you saying to change the crossfilter relationship from my 'Jobs' table to 'calendar' table to "both"? 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.