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
Applicable88
Impactful Individual
Impactful Individual

Allocate shift time to Transaction table

Hello,

 

I know many use the concepts of is-statment to allocate certain times to the right shift time. But is it possible to use a prepared 

shift time table to allocate the right shift to the transaction table?

For example my allocation table only consist of three rows:

Applicable88_0-1623793670345.png

Now I don't know how to combine this with the Datetime column "StartingTime" of my transactional table, since there is no If smaller or if greater than 6:00am etc...since joins are for exact matches.

How to tell power query when Starting time is between 22:00 to 6am its a nightshift and so fourth with this model?

 

Thank you very much in advance.

Best. 

1 ACCEPTED SOLUTION
Vera_33
Resident Rockstar
Resident Rockstar

Hi @Applicable88 

 

I believe there are many ways to do it, you can write nested if then else. Here is one way

 

sort the reference table, mine is called ShiftTable

Vera_33_0-1623906399223.png

 

Vera_33_1-1623906446539.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdG7CcMADEXRVYLrQPTkv1cx3n+NpAgYwwFVtxAc6TyHyuc3XZ1XzcdYw/V+xkVxVdwUd8SUYhRbcVScFCWKRJEoEkWilqglaon6FvV9ulKMYity56Q4Ky6Kq+KmuCNGokgUiSJRJIpEkSgSRaJI1BK1RP+/X18=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [StartDate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"StartDate", type datetime}}),

    fnSwitch = (input) => let
        values = List.Zip({ShiftTable[From],ShiftTable[Shift]})
    in
        try List.First(List.Select(values, each _{0 }< input)){1} otherwise "Night",

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each fnSwitch(DateTime.Time([StartDate])))
in
    #"Added Custom"

 

 

 

View solution in original post

5 REPLIES 5
Vera_33
Resident Rockstar
Resident Rockstar

Hi @Applicable88 

 

I believe there are many ways to do it, you can write nested if then else. Here is one way

 

sort the reference table, mine is called ShiftTable

Vera_33_0-1623906399223.png

 

Vera_33_1-1623906446539.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdG7CcMADEXRVYLrQPTkv1cx3n+NpAgYwwFVtxAc6TyHyuc3XZ1XzcdYw/V+xkVxVdwUd8SUYhRbcVScFCWKRJEoEkWilqglaon6FvV9ulKMYity56Q4Ky6Kq+KmuCNGokgUiSJRJIpEkSgSRaJI1BK1RP+/X18=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [StartDate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"StartDate", type datetime}}),

    fnSwitch = (input) => let
        values = List.Zip({ShiftTable[From],ShiftTable[Shift]})
    in
        try List.First(List.Select(values, each _{0 }< input)){1} otherwise "Night",

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each fnSwitch(DateTime.Time([StartDate])))
in
    #"Added Custom"

 

 

 

Hi @Syndicate_Admin 

 

You are asking the same question, I have my answer below you can check. It was generated by Power Query, I simply entered the sample data. The only custom function is that fnSwitch, and you need you need a reference table (mine was called ShiftTable) as another query

@Vera_33 , thanks for the quick reply. Can you explain to me the following part. What is that?: 

inary.FromText("bdG7CcMADEXRVYLrQPTkv1cx3n+NpAgYwwFVtxAc6TyHyuc3XZ1XzcdYw/V+xkVxVdwUd8SUYhRbcVScFCWKRJEoEkWilqglaon6FvV9ulKMYity56Q4Ky6Kq+KmuCNGokgUiSJRJIpEkSgSRaJI1BK1RP+/X18=", BinaryEncoding.Base64)

I used a sample data here and cannot put it into my actual data to use. 

Did you wrote the code and can you get there via "clicking" only in PowerQuery? 

Best. 

Hi @Applicable88 

 

It was done by Power Query via Enter Data, simply to put your sample data

 

Vera_33_0-1624262536298.png

 

if you get a blank query, and paste all the M code to Advanced Editor, you can see the steps, but I did not put the reference table here. So to see the output you need a reference table called ShiftTable as another query, hope it is clear

 

Vera_33_1-1624262595746.png

 

I wrote this custom function, if your table is not called ShiftTable, and column names are different, please do modify accordingly

 

    fnSwitch = (input) => let
        values = List.Zip({ShiftTable[From],ShiftTable[Shift]})
    in
        try List.First(List.Select(values, each _{0 }< input)){1} otherwise "Night",

 

@Vera_33 , thank you very much 🙂

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
Top Kudoed Authors