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
Anonymous
Not applicable

Assign Team based on Date

Hello,

 

I am having hard time assigning teams from an Employee reference table that has the employee name, their assign teams and date they join that time. I am using Lookupvalue to get the assign team and adding that to the sales table. But when I try to do this, a duplicate error shows up.

 

This is how the tables look:

learn2fly_0-1635489065710.png

 

learn2fly_1-1635489091807.png

 

This is how it should look(I use Sql to join the two tables):

 

 

Select S.*,
       EmpRef.Team


From Sales as S
OUTER APPLY(
SELECT TOP 1 E.Team
FROM EmployeeRefernce as E
WHERE	E.Employee = S.Employee
		AND E.EffectiveDay <= S.CreateDay
ORDER BY E.EffectiveDay DESC) AS EmpRef

 

 

 

learn2fly_2-1635489757993.png

 

Please any assist or help is greatly appreciated.Thank you so much!

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

Write these 2 calculated columns in the Sales table

Applicable effective day = CALCULATE(MAX('Employee reference'[EffectiveDay]),FILTER('Employee reference','Employee reference'[Employee]=EARLIER(Sales[Employee])&&'Employee reference'[EffectiveDay]<=EARLIER(Sales[CreateDay])))

 

Team = LOOKUPVALUE('Employee reference'[Team],'Employee reference'[EffectiveDay],Sales[Applicable effective day],'Employee reference'[Employee],Sales[Employee])

 

Hope this helps.

 

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Hello,

 

I am having hard time assigning teams from an Employee reference table that has the employee name, their assign teams and date they join that time. I am using Lookupvalue to get the assign team and adding that to the sales table. But when I try to do this, a duplicate error shows up.

 

This is how the tables look:

learn2fly_0-1635489065710.png

 

learn2fly_1-1635489091807.png

 

This is how it should look(I use Sql to join the two tables):

 

 

Select S.*,
       EmpRef.Team


From Sales as S
OUTER APPLY(
SELECT TOP 1 E.Team
FROM EmployeeRefernce as E
WHERE	E.Employee = S.Employee
		AND E.EffectiveDay <= S.CreateDay
ORDER BY E.EffectiveDay DESC) AS EmpRef

 

 

 

learn2fly_2-1635489757993.png

 

Please any assist or help is greatly appreciated.Thank you so much!

Hey @Anonymous

 

please do not crosspost your question across multiple forums.

See my remark to your question in the Desktop forum.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
AlexisOlson
Super User
Super User

You can do this in the query editor by merging on Employee, expanding the other two columns, filtering for EffectiveDay <= CreateDay, then doing a group by to take the team corresponding with the maximal remaining EffectiveDay.

 

Here's what the M code for this looks like:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8spPVdJRMjTUN9I3MjAyALJNzZRidaKVfPOh4qYwCRMDsIRPYlElkGeB0GFoaAiWCcnMBfLMERJGEB0odhhaguwwQLIDaoUhiG2AYoehEYZZKHaAtJgqxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Employee = _t, CreateDay = _t, UniteSold = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee", type text}, {"CreateDay", type date}, {"UniteSold", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Employee"}, EmployeeRefernce, {"Employee"}, "EmployeeRefernce", JoinKind.LeftOuter),
    #"Expanded EmployeeRefernce" = Table.ExpandTableColumn(#"Merged Queries", "EmployeeRefernce", {"EffectiveDay", "Team"}, {"EffectiveDay", "Team"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded EmployeeRefernce", each ([EffectiveDay] <= [CreateDay])),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Employee", "CreateDay", "UniteSold"}, {{"Team", each Table.Max(_, "EffectiveDay")[Team], type text}})
in
    #"Grouped Rows"

(Note that this won't work unless you already have the EmployeeRefernce table defined.)

 

DAX may be a bit simpler. You can define a calculated column like this:

Team = 
VAR LastEffectiveDay =
    CALCULATE (
        MAX ( EmployeeRefernce[EffectiveDay] ),
        EmployeeRefernce[Employee] = EARLIER ( Sales[Employee] ),
        EmployeeRefernce[EffectiveDay] <= EARLIER ( Sales[CreateDay] )
    )
RETURN
    CALCULATE (
        SELECTEDVALUE ( EmployeeRefernce[Team] ),
        EmployeeRefernce[Employee] = EARLIER ( Sales[Employee] ),
        EmployeeRefernce[EffectiveDay] = LastEffectiveDay
    )

 

Ashish_Mathur
Super User
Super User

Hi,

Write these 2 calculated columns in the Sales table

Applicable effective day = CALCULATE(MAX('Employee reference'[EffectiveDay]),FILTER('Employee reference','Employee reference'[Employee]=EARLIER(Sales[Employee])&&'Employee reference'[EffectiveDay]<=EARLIER(Sales[CreateDay])))

 

Team = LOOKUPVALUE('Employee reference'[Team],'Employee reference'[EffectiveDay],Sales[Applicable effective day],'Employee reference'[Employee],Sales[Employee])

 

Hope this helps.

 

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
TomMartens
Super User
Super User

Hey @Anonymous ,

 

Maybe, you can consider to desribe the overall requirement in more detail and also to create a pbix file that contains sample data, but also reflects your data model (tables, relationships, calculated columns, and measures that might impact the result of the expected result). Upload the pbix to onedrive or dropbox and share the link. If you are using Excel to create the sample data, instead of using the manual input method, share the xlsx as well.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

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.