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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Bonjuga
Frequent Visitor

DAX Calculating Average

Hi Guys,

 

I have a database that has data looks like following:

Transaction IDStatusDays
1received1
1sent2
2received3
2sent4
2received from IR5
3received6
3sent7
3received from IR8
4received9
4sent10

so, for each transaction, there are either two or three status: :"received", "sent", or "received from IR".  Now I am very interested in average time between "received from IR" and "sent".  That means, I need to extract those Transactions that have " received from IR"(for example, transaction 2 and 3), then use "received from IR" days minus "sent" days. 

 

The expected output would be ((5-4)+(8-7))/2, so it is (("received from IR" from transaction 2- "sent" from transaction2)+("received from IR" from transaction 3 - "sent" from transaction 3))/2

 

Is there anyway I could do that with DAX?  Also, I am currently using Azure database, which means I cannot create new column, I can only create new meausres.

 

Please let me know if there are any questions.  Thanks.

1 ACCEPTED SOLUTION
v-yulgu-msft
Employee
Employee

Hi @Bonjuga,

 

Please try below measures:

days for received IR =
CALCULATE (
    SUM ( Sheet2[Days] ),
    FILTER ( Sheet2, Sheet2[Status] = "received from IR" )
)

days for sent =
CALCULATE (
    SUM ( Sheet2[Days] ),
    FILTER (
        FILTER (
            Sheet2,
            CALCULATE (
                COUNT ( Sheet2[Transaction ID] ),
                ALLEXCEPT ( Sheet2, Sheet2[Transaction ID] )
            )
                = 3
        ),
        Sheet2[Status] = "sent"
    )
)

Count transaction =
CALCULATE (
    DISTINCTCOUNT ( Sheet2[Transaction ID] ),
    FILTER ( Sheet2, Sheet2[Status] = "received from IR" )
)

Average = ([days for received IR]-[days for sent])/[Count transaction]

Best regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

7 REPLIES 7
v-yulgu-msft
Employee
Employee

Hi @Bonjuga,

 

Please try below measures:

days for received IR =
CALCULATE (
    SUM ( Sheet2[Days] ),
    FILTER ( Sheet2, Sheet2[Status] = "received from IR" )
)

days for sent =
CALCULATE (
    SUM ( Sheet2[Days] ),
    FILTER (
        FILTER (
            Sheet2,
            CALCULATE (
                COUNT ( Sheet2[Transaction ID] ),
                ALLEXCEPT ( Sheet2, Sheet2[Transaction ID] )
            )
                = 3
        ),
        Sheet2[Status] = "sent"
    )
)

Count transaction =
CALCULATE (
    DISTINCTCOUNT ( Sheet2[Transaction ID] ),
    FILTER ( Sheet2, Sheet2[Status] = "received from IR" )
)

Average = ([days for received IR]-[days for sent])/[Count transaction]

Best regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks!!!!!!! That works!!!!!!! You just solved a huge problem of mine!! Thanks!!!!!

Bonjuga
Frequent Visitor

Hi Guys,

 

I have a database that has data looks like following:

Transaction IDStatusDays
1received1
1sent2
2received3
2sent4
2received from IR5
3received6
3sent7
3received from IR8
4received9
4sent10

so, for each transaction, there are either two or three status: :"received", "sent", or "received from IR".  Now I am very interested in average time between "received from IR" and "sent".  That means, I need to extract those Transactions that have " received from IR"(for example, transaction 2 and 3), then use "received from IR" days minus "sent" days. 

 

The expected output would be ((5-4)+(8-7))/2, so it is (("received from IR" from transaction 2- "sent" from transaction2)+("received from IR" from transaction 3 - "sent" from transaction 3))/2

 

Is there anyway I could do that with DAX?  Also, I am currently using Azure database, which means I cannot create new column, I can only create new meausres.

You should be able to use CALCULATE with a measure to do what you want. So, for example:

 

Measure = CALCULATE(SUM([Days]),FILTER(Table,[Status]="received"))

You could create a similar one for "sent" and then perhaps a third measure to do your calculation?


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thanks for your reply!!  However, I don't want transaction 1 and 4, how can I filter them out so they won't affect my calculation?

GilbertQ
Super User
Super User

Hi there, thanks for the details, would it be possible to post what you expect the output to look like?

Makes it easier to find a working solution.




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

Proud to be a Super User!







Power BI Blog

Thanks for your advice!  Just updated question!

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.