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
bkoenen
Helper I
Helper I

DAX Calculate Countrows, average per Year per Weekday

Hello,

 

I think I have a simple question but I can't figure out how to do it.

 

I have a table in BI for example with a Date column and a column with the matching weekdays. There can be multiple equal dates because it's an order picking table.

 

I would like to have that it counts the rows from the same Weekday and divide it by the matching dates. So when I have 6 rows in total on Mondays and these Mondays matches two dates, the calculation should be 6 / 2 Then my average = 3 (check table as example)

 

What should be the correct DAX formula for this?

 

DateDay of the week  
2022-01-03Monday   
2022-01-03Monday   
2022-01-03Monday   
2022-01-03Monday   
2022-01-04Tuesday   
2022-01-04Tuesday   
2022-01-05Wednesday   
2022-01-05Wednesday   
2022-02-07Monday   
2022-02-07Monday   
2022-02-08Tuesday   
2022-02-09Wednesday   
2022-02-10Thursday   
2022-02-10Thursday   
etc. etc.    
     
Average Countrows per Year per Weekday 
 ResultCalculation  
Monday36 rows on Monday / 2 Dates
Tuesday1,53 rows on Tuesday / 2 Dates
Wednesday1,53 Rows on Wednesday / 2 Dates
Thursday22 Rows on Thursday / 1 Date

 

With kind regards,

 

Björn Koenen

2 ACCEPTED SOLUTIONS
tackytechtom
Super User
Super User

Hi @bkoenen ,

 

How about this:

tomfox_0-1649188606196.png

 

 

Here the measure:

Average Countrows per Year per Weekday = 
VAR _helpTable = 
SUMMARIZE (
    Table,
    Table[Day of the week],
    "NumberOfDayOfTheWeek", COUNT ( Table[Day of the week] ),
    "DistinctNumberOfDates", DISTINCTCOUNT ( Table[Date] )
) 
RETURN
MAXX (_helpTable, [NumberOfDayOfTheWeek] ) / MAXX (_helpTable, [DistinctNumberOfDates] ) 

 

And here how the helpTable would look like:

tomfox_1-1649188713265.png

 

Let me know if this helps or if you have any questions 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/

 



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

View solution in original post

v-zhangti
Community Support
Community Support

Hi, @bkoenen 

 

You can try the following methods.

Measure:

Result = 
VAR _N1 =
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[Date] ),
        FILTER (
            ALL ( 'Table' ),
            [Day of the week] = SELECTEDVALUE ( 'Table'[Day of the week] )
        )
    )
VAR _N2 =
    CALCULATE (
        COUNT ( 'Table'[Day of the week] ),
        FILTER (
            ALL ( 'Table' ),
            [Day of the week] = SELECTEDVALUE ( 'Table'[Day of the week] )
        )
    )
RETURN
    DIVIDE ( _N2, _N1 )

vzhangti_0-1649406013229.png

 

COUNT: https://docs.microsoft.com/dax/count-function-dax 

DISTINCTCOUNT: https://docs.microsoft.com/dax/distinctcount-function-dax 

 

Best Regards,

Community Support Team _Charlotte

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

9 REPLIES 9
v-zhangti
Community Support
Community Support

Hi, @bkoenen 

 

You can try the following methods.

Measure:

Result = 
VAR _N1 =
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[Date] ),
        FILTER (
            ALL ( 'Table' ),
            [Day of the week] = SELECTEDVALUE ( 'Table'[Day of the week] )
        )
    )
VAR _N2 =
    CALCULATE (
        COUNT ( 'Table'[Day of the week] ),
        FILTER (
            ALL ( 'Table' ),
            [Day of the week] = SELECTEDVALUE ( 'Table'[Day of the week] )
        )
    )
RETURN
    DIVIDE ( _N2, _N1 )

vzhangti_0-1649406013229.png

 

COUNT: https://docs.microsoft.com/dax/count-function-dax 

DISTINCTCOUNT: https://docs.microsoft.com/dax/distinctcount-function-dax 

 

Best Regards,

Community Support Team _Charlotte

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

tackytechtom
Super User
Super User

Hi @bkoenen ,

 

How about this:

tomfox_0-1649188606196.png

 

 

Here the measure:

Average Countrows per Year per Weekday = 
VAR _helpTable = 
SUMMARIZE (
    Table,
    Table[Day of the week],
    "NumberOfDayOfTheWeek", COUNT ( Table[Day of the week] ),
    "DistinctNumberOfDates", DISTINCTCOUNT ( Table[Date] )
) 
RETURN
MAXX (_helpTable, [NumberOfDayOfTheWeek] ) / MAXX (_helpTable, [DistinctNumberOfDates] ) 

 

And here how the helpTable would look like:

tomfox_1-1649188713265.png

 

Let me know if this helps or if you have any questions 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/

 



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

Hello Tom,

 

Looks good, but I get everywhere the same result? Has it something to do with the MAXX function?

 

bkoenen_0-1649231145537.png

 

 

Kind regards Björn

Hi @bkoenen ,


I created a measure instead of a calculated column 🙂

 

try a measure instead and see whether it works!

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

Hello Tom,

 

When I use the example with the Measure it works correct, but when I use it in the original report the average results are different then when you calculate it.

 

Left results are the picking orders per Monday, there are 13 Mondays. Calculation: 9392 / 13 = 722

 

Our calculation says there is an average of 2789 on Mondays? Any idea what goed wrong?

 

bkoenen_0-1649235408129.png

 

 

Kind regards Björn

[Your Column] = // a calc column, not a measure
var thisYear = year( T[Date] )
var dayOfWeek = T[Day of Week]
var RowsOfInterest =
    filter(
        T,
        year( T[Date] ) = thisYear
        &&
        T[Day Of Week] = dayOfWeek
    )    
var numOfSameWeekdays = countrows( RowsOfInterest )
var numOfDifferentDates =
    distinctcount(
        selectcolumns(
            RowsOfInterest,
            "@Date", T[Date]
        )
    )
var ratio = numOfSameWeekdays / numOfDifferentDates
return
    ratio

Hello daXtreme,

 

I get an error message for the Distinctcount function, any idea?

 

bkoenen_0-1649251478841.png

 

Kind regards Björn

Change to this:

[Your Column] = // a calc column, not a measure
var thisYear = year( T[Date] )
var dayOfWeek = T[Day of Week]
var RowsOfInterest =
    filter(
        T,
        year( T[Date] ) = thisYear
        &&
        T[Day Of Week] = dayOfWeek
    )    
var numOfSameWeekdays = countrows( RowsOfInterest )
var numOfDifferentDates =
    countrows(
        distinct(
            selectcolumns(
                RowsOfInterest,
                "@Date", T[Date]
            )
        )
    )
var ratio = numOfSameWeekdays / numOfDifferentDates
return
    ratio

Hello,

 

I did a test with a smaller amount of rows(20000). There are 2 dates on a Monday in the DATUM column, I took as return result only the VAR "numOfDifferentDates" to see the output. This gives 1704 and should be 2(Mondays) if we got this one correct and we put back the formula then it would be correct.

 

 

bkoenen_1-1649335108240.png

 

 

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.

Top Solution Authors