bin() adx scalar function returns an aggregate every round hour but I want it to be on the half hour - azure

| take 2000000
| summarize Value = avg(Value) by bin(TimeStampOfValue, 1h)
I have an adx table with Value and a Timestamp and I run this query I get the avg Value every hour for example:
TimeStampOfValue
Value
2022-01-30T22:00:00
500
2022-01-30T23:00:00
499,99
I'd like it to return:
TimeStampOfValue
Value
2022-01-30T22:30:00
500
2022-01-30T23:30:00
499,99
How do I shift the 'by bin' by 30 minutes? so it runs hourly on the half hour mark? Is this even possible?
One solution is to use 'bin_at' with a specific time so it starts hourly from there, is this the only way?

Related

Kusto: make-series stops with first day - doesnt work as expected

I am using azure customer metrics to store application usage metrics, I am exporting the stats every 5 minutes. I am using the query below to create a aggregated series without any gaps.
I expect the start to be 5/10/2020, 12:00:00.000 AM and end to be 5/14/2020, 12:00:00.000 AM. However in my results, start is fine , but the end is 5/10/2020, 10:35:00.000 AM. I am running this query on 5/13/2020, 4:09:07.878 AM. The min timestamp in my data is 5/11/2020, 12:54:06.489 PM and max is 5/12/2020, 2:32:47.459 PM.
What is wrong with my query? why the make-series wouldn't give rows beyond day 1
let start = floor(ago(1d),1d);
let end = floor(now(+1d),1d);
customMetrics
| where timestamp >= start
| where name == "endpoint_access_count_count_view"
| extend customMetric_valueSum = iif(itemType == 'customMetric',valueSum,todouble(''))
| make-series n_req = sum(customMetric_valueSum) on timestamp from start to end step 5m
| mvexpand n_req,timestamp
| extend timestamp=todatetime(timestamp),n_req=toint(n_req)
mvexpand, unlike mv-expand (note the hyphen), has a default limit of 128 values, so your results get truncated.
https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/mvexpandoperator

Summing time fields over 24 hours in Power Query

I have a Power Query in excel linked to another file. This file has a time column. I understand that M language will not sum above 24 hours automatically without some work as it uses a datetime reference hence if I import a time of 25 hours it reverts back 2 hours to 1 hour...
In the 3rd column along in my image below using the second row as a reference, this is actually supposed to read 47:47:38. How can I get the instances where the value is above 24 hours to show the true hours?
I have tried using duration.hours(#hours()) this also does not work for some reason.
The same data from the source excel file is below also
Power Query doesn't have custom formats for how it displays data. If you have it read your data as a Duration instead of a DateTime it will display as [d].hh.mm.ss format, but still not with the total hours. Ultimately though this doesn't really matter because even when your data is formatted to display total hours in Excel, it's really being stored internally as days+hours+minutes+seconds. So how it displays in Power Query doesn't matter, as you can just use the hour formatting wherever you output the data to.
Now if you need to use the hours for a calculation between something that isn't another Duration, you can extract the hours by doing
Duration.Days([Your Hours]) * 24 + Duration.Hours([Your Hours])
Or now that I look at it, there is also a TotalHours function that gives you the hours plus mm:ss as a fractional amount of that
Duration.TotalHours([Your Hours])
Power BI doesn't handle this case very gracefully. A solution could be to convert the duration to a number to make it additive (so you can perform calculations and aggregations) and when you need to visualize it, to convert it to the desired format (HH:MM:SS).
Duration and Time are often confused. When such Excel files are read, the type of the column usually is DateTime, and date 1899-12-31 is added to the "time" part. You can change the data type of the column to be Decimal Number, but the "zero point" in Excel unfortunately is one day off (1899-12-30), so you need to subtract 1 from the result to get the actual "number of days" of the duration (i.e. 0.25 means 06:00:00).
So you must perform some conversion of the data. I would make a new column in the model to get the duration in the lowest granularity that I need (seconds in your example). In Power Query Editor add a custom column to calculate the duration in seconds (where Column1 is the name of the original duration column):
Duration in seconds = Duration.TotalSeconds([Column1] - #datetime(1899, 12, 31, 0, 0, 0))
Make sure the data type of this column is Whole Number (change it if necessary). Here 9144 seconds are calculated as 2 * 3600 + 32 * 60 + 24, or 02:32:24. Now you can calculate a sum on this column to get total duration in seconds for example. But when you visualize this column, don't do it directly, but make a measure to convert the data to the desired format. It could me made like this:
Measure Duration =
VAR duration_in_seconds = SUM(Sheet1[Duration in seconds])
VAR hours = ROUNDDOWN ( duration_in_seconds / 3600; 0 )
VAR minutes = ROUNDDOWN ( MOD ( duration_in_seconds; 3600 ) / 60; 0 )
VAR seconds = INT ( MOD ( duration_in_seconds; 60 ) )
RETURN hours & ":" & FORMAT(minutes; "00") & ":" & FORMAT(seconds; "00")
duration_in_seconds variable hold the total duration in seconds of the data in the context. From it we are calculating hours, minutes and seconds and constructing a string to represent the duration in the desired format. FORMAT is used to make sure there is a leading zero in case minutes or seconds are less than 10.
Here is how all three columns looks like when visualized:
Hope this helps!

Powerpivot Dax- Calculate number of Hours after a fixed time

I have a list of times in a column [Logtime]:
11:45:44PM
07:05:05PM
I'd like to create a measure that returns the total number of hours after 6:30PM. So given the above times:
5.5
0.58
HoursAfter:=[logtime] -6:30PM doesn't work.
Hour[logtime] - hour(18.5) also doesn't work
EDIT:
timevalue([logtime]) - timevalue("05:00:00") works but it returns a datetime ala
12/30/1899 5:17:16PM
I need to convert the time 5:17:16 into decimal hours i.e. 5.26, how can I do this?
There may be a slightly more elegant way, but I was able to create a calculated column that does what you want as follows,
HoursAfter = DATEDIFF(TIMEVALUE("6:30 PM"), Times[LogTime], SECOND) / 3600
This takes the time difference between 6:30 PM and your LogTime in units of seconds and then converts it to hours by dividing by 60*60 = 3600.
Edit: A simpler formula can be written as follows,
HoursAfter = 24 * (Times[LogTime] - TIMEVALUE("6:30 PM"))
(Multiply by 24 since the datetime values are stored in units of days.)
timevalue([logtime]) - timevalue("05:00:00")

Powerpivot DAX time- counting when a timestamp falls within 1st 30 mins or 2nd 30 mins of an hour

say I have a list of users and timestamps.
User Time
Tim 6:15PM
Tim 6:17PM
Tim 6:44PM
Tim 3:33PM
Bort 8:00PM
Bort 9:04PM
Bort 9:05PM
I want to count, for each user, the number of 30 minute increment they have a timestamp for, within each hour between 5PM and 8 30AM
Result:
Tim 2
Bort 2
Note that Tim has times at 6:15 and 6:17, this falls within the first 30 minutes of the 6PM hour, therefore it only counts as 1. He has a time at 6:44PM, which comes after 6:30PM, therefore it also counts as 1. Altogether he has 2
Bort has a time in the first half of 8PM but not in the latter half. So he gets 1. He then has two times within 9PM. Altogether he has 2.
Is this too intense to do in Powerpivot/dax?
I'm assuming you meant PM for those last two Bort rows since that's how you referred to it later.
I was able to achieve this by doing the following:
Create a calculated column HalfHour = ROUNDUP(48*TimeStamps[Time],0).
Create a measure CountTimeStamps as below.
Put them in a matrix visual with User on the rows and CountTimeStamps in the values.
CountTimeStamps =
CALCULATE(
DISTINCTCOUNT(TimeStamps[HalfHour]),
TimeStamps[Time] <= TIMEVALUE("8:30AM") ||
TimeStamps[Time] >= TIMEVALUE("5PM")
)

Excel Function For If Then Statement for Range of Times

=IF(I44<"0:01","0",IF(I44<"0:30","2:00",IF(I44<"1:00","2:30",IF(I44<"1:30","3:00",IF(I44<"2:00","3:30",IF(I44<"2:30","4:00",IF(I44<"3:00","4:30",IF(I44<"3:30","5:00",IF(I44<"4:00","5:30",IF(I44<"4:30","6:00",IF(I44<"5:00","6:30",IF(I44<"5:30","7:00",IF(I44<"6:00","7:30",IF(I44<"6:30","8:00",IF(I44<"7:00","8:30",IF(I44<"7:30","9:00",IF(I44<"8:00","9:30",IF(I44<"8:30","10:00",IF(I44<"9:00","10:30",IF(I44<"9:30","11:00",IF(I44<"10:00","11:30","")))))))))))))))))))))
(Left out the code tags so you dont have to scroll 30 pages to the right)
I am using this function to add specific amounts of time to specific range of time and this seems like there must be a better method of doing this.
For example: for a input time of < 30 min, would output 2:00 hr, for input time of < 1:00 would output 2:30 ... and for each 30 min increment in the input the output would increment by 30 minutes
Perhaps just round down to the next half hour then add 2 hours, i.e
=IF(I44=0,0,FLOOR(I44,"0:30")+"2:00")
[with an IF to deal with zero values]
Format result cell as a time value, e.g. h:mm or similar

Resources