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
b2wise
Helper III
Helper III

Count With Multiple Conditions - DAX

Hi,

 

I'm trying to count orders that must ship today using a DAX formula and am getting no results.

Must ship orders are determined by:

  • Date created
  • Time created
  • Host priority

The business requirements are as follows: Count all orders with host priority 10-29 as long as they weren't created after 15:15 today and count all orders host priority 9 and lower as long as they weren't created after 16:15 today.

 

I made an order count formula:

Order Count = DISTINCTCOUNTNOBLANK(Headers[Fulfillment No])
 
And placed it in this formula:
 
Must Ship Orders =
CALCULATE (
[Order Count],
FILTER (
Headers,
SWITCH (
TRUE (),
Headers[Date Created] = MAX ( 'Current Date Time'[Date] )
&& ( Headers[Time Created] > "15:15" ), NOT ( Headers[Host Priority] >= "10" ),
Headers[Date Created] = MAX ( 'Current Date Time'[Date] )
&& ( Headers[Time Created] > "16:15" ), NOT ( Headers[Host Priority] < "10" )
)
)
)

 

What am I doing wrong?

 

Here is what my data looks like:

 

image.png

 

1 ACCEPTED SOLUTION
v-xiaotang
Community Support
Community Support

Hi @b2wise 

I create a sample file under DQ mode, here is my operation steps.

-

Change the type of column [Date Created] to Date

v-xiaotang_0-1622014079429.png

Since type of your 3 columns is text and can’t be changed, so create the measures bellow.

 

 

 

sortbytime = //we need to create this measure so that we can sort by [Time Created], because type of this column is text
VAR _timecreated =
    SELECTEDVALUE ( Headers[Time Created] )
RETURN
    VALUE ( LEFT ( _timecreated, 2 ) ) * 100
        + VALUE ( MID ( _timecreated, 4, 2 ) )
get priority = //the reason to create this measure is same as the measure above
VAR _pri =
    SELECTEDVALUE ( Headers[Host Priority] )
VAR _lenvalue =
    IF ( LEN ( _pri ) > 1, LEN ( _pri ) - 1 )
VAR _getnumfromtext =
    RIGHT ( _pri, _lenvalue )
VAR _vau =
    IF ( ISERROR ( VALUE ( _pri ) ), _getnumfromtext, VALUE ( _pri ) )
RETURN
    _vau

 

 

then, create the measure count and put it in the Card visual.

 

 

count =
VAR _timecreated =
    SELECTEDVALUE ( Headers[Time Created] )
VAR _time =
    VALUE ( LEFT ( _timecreated, 2 ) ) * 100
        + VALUE ( MID ( _timecreated, 4, 2 ) )
VAR _count =
    CALCULATE (
        COUNTROWS ( Headers ),
        FILTER (
            ALL ( Headers ),
            (
                Headers[Date Created] < DATE ( 2021, 5, 25 )//I replace your MAX ( 'Current Date Time'[Date] ) with DATE ( 2021, 5, 25 ), because I dont know the structure of 'Current Date Time'[Date], you can change it back later.
                    && [get priority] >= 1
                    && [get priority] <= 29
            )
                || (
                    Headers[Date Created] = DATE ( 2021, 5, 25 )
                        && [get priority] >= 10
                        && [sortbytime] < 1515
                )
                || (
                    Headers[Date Created] = DATE ( 2021, 5, 25 )
                        && [get priority] < 10
                        && [sortbytime] < 1615
                )
        )
    )
RETURN
    _count

 

 

 

Result:

v-xiaotang_1-1622014079433.png

 

Best Regards,

Community Support Team _ Tang

If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-xiaotang
Community Support
Community Support

Hi @b2wise 

I create a sample file under DQ mode, here is my operation steps.

-

Change the type of column [Date Created] to Date

v-xiaotang_0-1622014079429.png

Since type of your 3 columns is text and can’t be changed, so create the measures bellow.

 

 

 

sortbytime = //we need to create this measure so that we can sort by [Time Created], because type of this column is text
VAR _timecreated =
    SELECTEDVALUE ( Headers[Time Created] )
RETURN
    VALUE ( LEFT ( _timecreated, 2 ) ) * 100
        + VALUE ( MID ( _timecreated, 4, 2 ) )
get priority = //the reason to create this measure is same as the measure above
VAR _pri =
    SELECTEDVALUE ( Headers[Host Priority] )
VAR _lenvalue =
    IF ( LEN ( _pri ) > 1, LEN ( _pri ) - 1 )
VAR _getnumfromtext =
    RIGHT ( _pri, _lenvalue )
VAR _vau =
    IF ( ISERROR ( VALUE ( _pri ) ), _getnumfromtext, VALUE ( _pri ) )
RETURN
    _vau

 

 

then, create the measure count and put it in the Card visual.

 

 

count =
VAR _timecreated =
    SELECTEDVALUE ( Headers[Time Created] )
VAR _time =
    VALUE ( LEFT ( _timecreated, 2 ) ) * 100
        + VALUE ( MID ( _timecreated, 4, 2 ) )
VAR _count =
    CALCULATE (
        COUNTROWS ( Headers ),
        FILTER (
            ALL ( Headers ),
            (
                Headers[Date Created] < DATE ( 2021, 5, 25 )//I replace your MAX ( 'Current Date Time'[Date] ) with DATE ( 2021, 5, 25 ), because I dont know the structure of 'Current Date Time'[Date], you can change it back later.
                    && [get priority] >= 1
                    && [get priority] <= 29
            )
                || (
                    Headers[Date Created] = DATE ( 2021, 5, 25 )
                        && [get priority] >= 10
                        && [sortbytime] < 1515
                )
                || (
                    Headers[Date Created] = DATE ( 2021, 5, 25 )
                        && [get priority] < 10
                        && [sortbytime] < 1615
                )
        )
    )
RETURN
    _count

 

 

 

Result:

v-xiaotang_1-1622014079433.png

 

Best Regards,

Community Support Team _ Tang

If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

mahoneypat
Employee
Employee

You are going to want to change your data types first.  Your Created Date is datetime and should be date, and your time and priority columns are Text (those won't work well in expression where you use <, >, etc.  Once you do that, you can use an expression like this:

 

Must Ship Orders =
VAR maxdate =
    MAX ( 'Current Date Time'[Date] )
RETURN
    CALCULATE (
        [Order Count],
        Headers[Date Created] = maxdate,
        FILTER (
            ALL ( Headers[Time Created], Headers[Host Priority] ),
            OR (
                Headers[Time Created] > TIME ( 15150 )
                    && Headers[Host Priority] <= 10,
                Headers[Time Created] > TIME ( 16150 )
                    && Headers[Host Priority] > 10
            )
        )
    )

 

Are you sure you only want to count those created on the max date?

 

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Thanks for the answer.

I left out a few points:

A. This is in direct query so I don't think I can change the data types

B. The priority column occasionalty has text in it (e.g. P3) so I think I need to leave it as text

C.  I need to count all orders host priority 1 to 29 if date created is older than today, if date created is today count all orders created before 15:15 if host priority >= 10 and all orders created before 16:15 if host priority is <10

 

Thanks again for your help

@mahoneypat sorry I'm new to the community and didn't realize you have to "@" someone so they see you replied. Can you see my last reply?

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.