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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Gangsta
Frequent Visitor

DAX columns for date when sales target was reached by staff and number of days worked reach target

I woulkd be so greatful, if one of the DAX experts can help with a DAX formula for 2 columns I want to create.

I have 2 tables.

table 1 is called 'Staff' with 4 staff (A to D) in colum Staff, there are columns for Items Sold, Days Worked, Days To Meet Target and Date Target Met.

The related table is called 'Rota' has  columns Staff, Items Sold and Date Worked.

I need a DAX to formula for 3 additional columns in Staff table:

  • Days Worked, this will count number of days each staff has worked.
  • Days To Meet Target, work out the number of days staff worked to meet the target of 6 sales, ignoring days after target reached.
  • Date Target Met, this will find the date when staff reached their target of 6 sales, ignoring days after target was reached.

In the Staff Table below,

Staff 'C' shows them meeting target of 6 after working 1 day, they met thier target on 2nd Feb, they worked 2 days in total. 

Staff 'D' shows them meeting target of 6 after working 2 days, the 2nd day they worked was 4th April, they worked 3 days in total.

 

Staff

StaffItems SoldDays WorkedDays To Meet TargetDate Target Met
A32  
B21  
C92102-Feb-20
D73204-Apr-20

 

Rota

StaffItems SoldDate Worked
A113-Jan-20
C602-Feb-20
B214-Feb-20
D318-Feb-20
A221-Mar-20
D304-Apr-20
C305-Jun-20
D101-Jul-20
1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@Gangsta PBIX is attached below signature. If this helps, please vote for my sticker! Banana Pickle Math - Microsoft Fabric Community

 

Days Worked = COUNTROWS( DISTINCT( SELECTCOLUMNS( RELATEDTABLE( Rota ), "__Date", [Date Worked] ) ) )


Days To Meet Target = 
    VAR __Target = 6
    VAR __Table = RELATEDTABLE( 'Rota' )
    VAR __Table1 =
        ADDCOLUMNS(
            RELATEDTABLE( 'Rota' ),
            "__Cumulative", 
                VAR __Date = [Date Worked]
                VAR __Table = FILTER( __Table, [Date Worked] <= __Date )
                VAR __Result = SUMX( __Table, [Items Sold] )
            RETURN
                __Result
        )
    VAR __Result = IF( MAXX( __Table1, [__Cumulative]) < __Target, BLANK(), COUNTROWS( FILTER( __Table1, [__Cumulative] < __Target ) ) + 1)
RETURN
    __Result



Date Target Met = 
    VAR __Target = 6
    VAR __Table = RELATEDTABLE( 'Rota' )
    VAR __Table1 =
        ADDCOLUMNS(
            RELATEDTABLE( 'Rota' ),
            "__Cumulative", 
                VAR __Date = [Date Worked]
                VAR __Table = FILTER( __Table, [Date Worked] <= __Date )
                VAR __Result = SUMX( __Table, [Items Sold] )
            RETURN
                __Result
        )
    VAR __Result = MINX( FILTER( __Table1, [__Cumulative] >= __Target ), [Date Worked] )
RETURN
    __Result

 



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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

3 REPLIES 3
Gangsta
Frequent Visitor

@Greg_Deckler Thank you so much, I done, am well pleased with your solution and voted for your sticker. A quick test of all three columns shows they work as required, although the 'Date Target Met' has the time which I won't need, and likely to get in the way of afew queries so would be good to have just the date, not sure if it is easy to add this in the formula or should I  do this in helper column that reformats it to just date.

 

Again am very greatful for you to helping me out on this, saved me alot of head scratching since admittingly I am very new to DAX.

@Gangsta Thanks! Easiest way to remove the time is to just change the Format for the column to Short date.

Greg_Deckler_0-1723019744351.png

 



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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...
Greg_Deckler
Super User
Super User

@Gangsta PBIX is attached below signature. If this helps, please vote for my sticker! Banana Pickle Math - Microsoft Fabric Community

 

Days Worked = COUNTROWS( DISTINCT( SELECTCOLUMNS( RELATEDTABLE( Rota ), "__Date", [Date Worked] ) ) )


Days To Meet Target = 
    VAR __Target = 6
    VAR __Table = RELATEDTABLE( 'Rota' )
    VAR __Table1 =
        ADDCOLUMNS(
            RELATEDTABLE( 'Rota' ),
            "__Cumulative", 
                VAR __Date = [Date Worked]
                VAR __Table = FILTER( __Table, [Date Worked] <= __Date )
                VAR __Result = SUMX( __Table, [Items Sold] )
            RETURN
                __Result
        )
    VAR __Result = IF( MAXX( __Table1, [__Cumulative]) < __Target, BLANK(), COUNTROWS( FILTER( __Table1, [__Cumulative] < __Target ) ) + 1)
RETURN
    __Result



Date Target Met = 
    VAR __Target = 6
    VAR __Table = RELATEDTABLE( 'Rota' )
    VAR __Table1 =
        ADDCOLUMNS(
            RELATEDTABLE( 'Rota' ),
            "__Cumulative", 
                VAR __Date = [Date Worked]
                VAR __Table = FILTER( __Table, [Date Worked] <= __Date )
                VAR __Result = SUMX( __Table, [Items Sold] )
            RETURN
                __Result
        )
    VAR __Result = MINX( FILTER( __Table1, [__Cumulative] >= __Target ), [Date Worked] )
RETURN
    __Result

 



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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

Find out what's new and trending in the Fabric Community.