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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
ksmith0411
Regular Visitor

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

ksmith0411_0-1642411709079.png

 

 

1 ACCEPTED SOLUTION
v-yingjl
Community Support
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.

View solution in original post

6 REPLIES 6
v-yingjl
Community Support
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.

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

ksmith0411_0-1642673152833.png

 

 

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"

 

vyingjl_0-1642749091110.png

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.

 

mahoneypat
Employee
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 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


watkinnc
Super User
Super User

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

 

 


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

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

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors
Top Kudoed Authors