Power Bi question

MikkoR

New Member
Joined
Mar 17, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hey,
I need help with Power BI.

I have a table called Time Periods. The table has three columns, StartTime, EndTime and ID.

I would like to calculate how much time has been spent on each calendar day.

How could I do this?

An example of the content of the table
1705925535753.png


An example of the result
1705925553965.png



-Mike
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    min = Number.RoundDown(Number.From(List.Min(Source[StartTime]))),
    max = Number.RoundDown(Number.From(List.Max(Source[EndTime]))),
    lst = {min..max},
    lst1 = List.Transform(lst, each Date.From(_)),
    TimeCalc = (dt, start, end)=> 
        let ns = Number.From(start), ne = Number.From(end), nd = Number.From(dt)
        in  if nd = Number.RoundDown(ns) then List.Min({1-Number.Mod(ns,1), ne-ns}) else 
            if nd > Number.RoundDown(ns) and nd < ne then List.Min({1, ne - nd}) else 0,
    lst2 = Table.ToRows(Table.SelectColumns(Source,{"StartTime", "EndTime"})),
    lst3 = List.Transform(lst1, each 
        {_, List.Accumulate(lst2, 0, (s,c)=> s + TimeCalc(_, c{0},c{1}))}),
    Result = Table.FromRows(lst3, {"Date", "Days"})
in
    Result

Book1.xlsm
ABCDEFG
1Table1Query Output
2IDStartTimeEndTimeDateDays
311/16/2024 15:57:531/19/2024 19:37:391/14/20240.855954815
421/19/2024 13:43:001/27/2024 23:34:011/15/20242
531/16/2024 00:12:501/18/2024 10:45:201/16/20243.325898287
641/14/2024 05:56:511/20/2024 07:42:231/17/20244
751/20/2024 19:14:471/28/2024 23:51:341/18/20243.969103368
861/21/2024 10:24:211/26/2024 07:24:571/19/20244.818597014
971/14/2024 21:30:351/21/2024 17:15:421/20/20244.80671912
1081/18/2024 11:29:491/27/2024 02:07:041/21/20246.285656794
1191/19/2024 10:15:521/25/2024 22:27:241/22/20245.842862604
12101/20/2024 17:05:551/22/2024 20:13:431/23/20245
131/24/20245
141/25/20244.935699028
151/26/20243.308997593
161/27/20242.070201968
171/28/20240.99414235
18
Sheet2
 
Upvote 0
That is certainly a working solution when the source is Excel. But the source I have is a SQL table that I have imported into Power Bi. I would like to do these actions in the Power Bi application.
So the source table TimePeriods is imported from SQL server
 
Upvote 0
So I have boards imported to Power Bi. I would like that function so that a new board is created with the result. How do I do that?

Tables:
1706168601928.png
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top