Forum Discussion

ksmith0411's avatar
ksmith0411
Regular Visitor
4 years ago
Solved

Switch function

Hi,

 

Im trying to do a swicth query between two times ie if Start time >=08:00:00 & End time <=09:00:00 then return 09:00:00 but im not having any joy

 

If anyone can help that woul dbe great.

Thanks

K

 

 

  • Hi ksmith0411 ,

    Whether you need to save the first row as variable which named 'Hour_Part' and create the calculated column like this:

    Column 2 =
    VAR Hour_Part =
        TIME ( HOUR ( Actual[Start Time] ), MINUTE ( Actual[Start Time] ), SECOND ( Actual[Start Time] ) )
    RETURN
        SWITCH (
            TRUE (),
            Hour_Part > TIME ( 8, 0, 0 )
                && Hour_Part < TIME ( 9, 0, 0 ), TIME ( 9, 0, 0 )
        )
    

     

    Best Regards,
    Community Support Team _ Yingjie Li
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

6 Replies

Replies have been turned off for this discussion
  • v-yingjl's avatar
    v-yingjl
    Community Support

    Hi ksmith0411 ,

    Whether you need to save the first row as variable which named 'Hour_Part' and create the calculated column like this:

    Column 2 =
    VAR Hour_Part =
        TIME ( HOUR ( Actual[Start Time] ), MINUTE ( Actual[Start Time] ), SECOND ( Actual[Start Time] ) )
    RETURN
        SWITCH (
            TRUE (),
            Hour_Part > TIME ( 8, 0, 0 )
                && Hour_Part < TIME ( 9, 0, 0 ), TIME ( 9, 0, 0 )
        )
    

     

    Best Regards,
    Community Support Team _ Yingjie Li
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • ksmith0411's avatar
      ksmith0411
      Regular Visitor

      Thank you so much that worked.

       

      I have another query you might be able to help with, I also need to create a table that looks like the below, however Im not sure where to start with a formula

       

       

      • v-yingjl's avatar
        v-yingjl
        Community Support

        Hi ksmith0411 ,

        Noticed that there are duplicated rows in the table so that the table visual and matirx visual would just show the aggreated value.

         

        Basically you can create the below query in Power Query editor:

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrC0MjBQcPRV0lEyNLAyNAUxY3WiQRxjmLgRiBkAFYeydZSMQRqhosYgnWBRUyS1pBgdCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Start Time" = _t, #"End Time" = _t]),
            #"Changed Type" = Table.TransformColumnTypes(Source,{{"Start Time", type time}, {"End Time", type time}}),
            #"Added Custom" = Table.AddColumn(#"Changed Type", "Time", each List.Times(#time(8,0,0),13,#duration(0,1,0,0))),
            #"Expanded Time" = Table.ExpandListColumn(#"Added Custom", "Time"),
            #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Time",{{"Time", type time}}),
            #"Added Custom1" = Table.AddColumn(#"Changed Type1", "Diff", each if [Time] >= #time(Time.Hour([Start Time]),0,0) and [Time] < [End Time]  then if [Time] > [Start Time] and [Time] = #time(Time.Hour([End Time]),0,0) then Duration.Minutes([End Time]- [Time]) else if [Time] < [Start Time] and [Time] = #time(Time.Hour([Start Time]),0,0) then Duration.Minutes(#time(Time.Hour([Start Time])+1,0,0) - [Start Time]) else 60 else 0),
            #"Changed Type2" = Table.TransformColumnTypes(#"Added Custom1",{{"Diff", Int64.Type}})
        in
            #"Changed Type2"

         

        Best Regards,
        Community Support Team _ Yingjie Li
        If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

         

  • Anonymous's avatar
    Anonymous
    Not applicable

    Try it this way--assume your prior step is named PriorStep:

     

    = Table.AddColumn(PriorStep, "Next Hour", each Time.StartOfHour(Time.From([End Time] + #duration(0,1,0,0))))

     

    --Nate

     

     

    • ksmith0411's avatar
      ksmith0411
      Regular Visitor

      HI Nate,

       

      Thanks for coming back to me, Im afraid Im a complete novice on power BI ive tried this but think I'm entering it in wrong.

      Thanks

      K

  • mahoneypat's avatar
    mahoneypat
    Microsoft Employee

    I agree that I would probably do this in the query editor, but here is a DAX column expression that should work.

     

    NewColumn =
    IF (
        AND (
            Actual[Start Time] >= TIME ( 800 ),
            Actual[Start Time] <= TIME ( 900 )
        ),
        TIME ( 900 ),
        BLANK ()
    )

     

    Pat