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
wildmight2017
Advocate II
Advocate II

Creating weekly measures/calc on a daily grain fact table

Good afternoon,

 

I'm trying to understand what's the best way to calculate measure on a weekly level from day grain

 

Question A: i'd like to receive a measure with value of 3 for employee 1  (instead of selecting Week column from time dim)

Question B (slightly more difficult): if daily flag >= 1 then 1 else 0.  so I'd like to see 1 for 1st employee and 0 for 2nd. 

 

Is there a way to accomplish this without additional data modeling using only DAX expressions? 

 

Here's the data set in a fact table:

 

employee_iddateflag
1201601011
1201601020
1201601031
1201601040
1201601050
1201601060
1201601071
2201601010
2201601020
2201601030
2201601040
2201601050
2201601060
2201601070

 

Thank you

 

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

HI @wildmight2017,

 

>>Question A: i'd like to receive a measure with value of 3 for employee 1  (instead of selecting Week column from time dim)

Does the 'value of 3' which mentioned above means the wednesday or 3th date?

 

If means 'wednesday', you can add a calculate column to store the 'day of week', then filter records based on this column.
Sample:

Day of Week = 
VAR year =
    LEFT ( [date], 4 )
VAR month =
    LEFT ( RIGHT ( [date], 4 ), 2 )
VAR day =
    RIGHT ( RIGHT ( [date], 4 ), 2 )
RETURN
    WEEKDAY ( DATE ( year, month, day ), 2 )

 

Wednesday Records = CALCULATETABLE(employee,employee[Day of Week]=3) 

 

7.PNG

 

If means 3th date, you can try to add a index column, then filter on it.

Index = COUNTROWS(FILTER(ALL(employee),[employee_id]=EARLIER(employee[employee_id])&&[date]<=EARLIER([date])))
3th Records = CALCULATETABLE(employee,employee[Index]=3) 

 

>>Question B (slightly more difficult): if daily flag >= 1 then 1 else 0.  so I'd like to see 1 for 1st employee and 0 for 2nd. 

You can refer to below formulas which about summary table with flag and get the rank.

Result =
VAR temp =
    ADDCOLUMNS ( employee, "Result", IF ( [flag] >= 1, 1, 0 ) )//add column to convert flag based on your condition.
VAR summary =
    SUMMARIZE (
        temp,
        [employee_id],
        "Count", SUMX ( FILTER ( temp, [employee_id] = EARLIER ( [employee_id] ) ), [Result] )//summary table with id and result.
    )
RETURN
    ADDCOLUMNS ( summary, "Rank", RANKX ( summary, [Count],, DESC ) )// add rank column

8.PNG 

 

 

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

1 REPLY 1
v-shex-msft
Community Support
Community Support

HI @wildmight2017,

 

>>Question A: i'd like to receive a measure with value of 3 for employee 1  (instead of selecting Week column from time dim)

Does the 'value of 3' which mentioned above means the wednesday or 3th date?

 

If means 'wednesday', you can add a calculate column to store the 'day of week', then filter records based on this column.
Sample:

Day of Week = 
VAR year =
    LEFT ( [date], 4 )
VAR month =
    LEFT ( RIGHT ( [date], 4 ), 2 )
VAR day =
    RIGHT ( RIGHT ( [date], 4 ), 2 )
RETURN
    WEEKDAY ( DATE ( year, month, day ), 2 )

 

Wednesday Records = CALCULATETABLE(employee,employee[Day of Week]=3) 

 

7.PNG

 

If means 3th date, you can try to add a index column, then filter on it.

Index = COUNTROWS(FILTER(ALL(employee),[employee_id]=EARLIER(employee[employee_id])&&[date]<=EARLIER([date])))
3th Records = CALCULATETABLE(employee,employee[Index]=3) 

 

>>Question B (slightly more difficult): if daily flag >= 1 then 1 else 0.  so I'd like to see 1 for 1st employee and 0 for 2nd. 

You can refer to below formulas which about summary table with flag and get the rank.

Result =
VAR temp =
    ADDCOLUMNS ( employee, "Result", IF ( [flag] >= 1, 1, 0 ) )//add column to convert flag based on your condition.
VAR summary =
    SUMMARIZE (
        temp,
        [employee_id],
        "Count", SUMX ( FILTER ( temp, [employee_id] = EARLIER ( [employee_id] ) ), [Result] )//summary table with id and result.
    )
RETURN
    ADDCOLUMNS ( summary, "Rank", RANKX ( summary, [Count],, DESC ) )// add rank column

8.PNG 

 

 

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

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.