SOLVED

Trying to understand bin_at

%3CLINGO-SUB%20id%3D%22lingo-sub-159468%22%20slang%3D%22en-US%22%3ETrying%20to%20understand%20bin_at%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-159468%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20all%3C%2FP%3E%0A%3CP%3EI%20know%20this%20is%20a%20silly%20question%20but%20i'm%20struggling%20to%20understand%20how%20and%20where%20to%20use%20bin%20and%20bin_at%3CBR%20%2F%3EI've%20read%20the%20docs%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.loganalytics.io%2Fdocs%2FLanguage-Reference%2FScalar-functions%2Fbin_at%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.loganalytics.io%2Fdocs%2FLanguage-Reference%2FScalar-functions%2Fbin_at%3C%2FA%3E()%3C%2FP%3E%0A%3CP%3Ebut%20I%20don't%20understand%20the%20FixedPoint%20value.%20What%20and%20how%20should%20I%20use%20it%3F%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EBin%20is%20used%20to%20round%20values%2C%20correct%3F%3C%2FP%3E%0A%3CP%3EBut%20why%20and%20how%20to%20use%20bin_at%3F%3CBR%20%2F%3E%3CBR%20%2F%3EBest%20regards%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-159468%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EQuery%20Language%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-161430%22%20slang%3D%22en-US%22%3ERe%3A%20Trying%20to%20understand%20bin_at%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-161430%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Dante%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20is%20not%20a%20silly%20question%20at%20all.%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F9172%22%20target%3D%22_blank%22%3E%40Stanislav%20Zhelyazkov%3C%2FA%3E%26nbsp%3Bnoted%20your%20question%20and%20also%20brought%20to%20my%20attention%20how%20confusing%20the%20behavior%20is%2C%20so%20I'd%20like%20to%20explain%20how%20it%20actually%20works%2C%20and%20will%20also%20push%20to%20update%20the%20documentation%20of%20it.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%2C%20I%20ran%20this%20query%20to%20get%20the%20latest%20CPU%20report%20on%20a%20Computer%20named%20%22ContosoWeb%22%3A%3C%2FP%3E%0A%3CPRE%3EPerf%0A%7C%20where%20ObjectName%20%3D%3D%20%22Processor%22%20and%20CounterName%20%3D%3D%20%22%25%20Processor%20Time%22%20and%20InstanceName%20%3D%3D%20%22_Total%22%20and%20Computer%20%3D%3D%20%22ContosoWeb%22%20%0A%7C%20summarize%20arg_max(TimeGenerated%2C%20*)%3C%2FPRE%3E%0A%3CP%3EThe%20results%20showed%20the%20latest%20records%20is%20from%2015%3A03%3A57.%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F28879iC4AE1D4CC4E5F5E5%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22arg_max.png%22%20title%3D%22arg_max.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20wanted%20to%20calculate%20the%20average%20CPU%20usage%20per%20hour%2C%20over%20the%20last%206%20hours%20(not%20shown%20in%20this%20query%2C%20selected%20in%20the%20UI)%2C%20so%20I%20used%20bin%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EPerf%0A%7C%20where%20ObjectName%20%3D%3D%20%22Processor%22%20and%20CounterName%20%3D%3D%20%22%25%20Processor%20Time%22%20and%20InstanceName%20%3D%3D%20%22_Total%22%20and%20Computer%20%3D%3D%20%22ContosoWeb%22%20%0A%7C%20summarize%20AVGCPU%20%3D%20avg(CounterValue)%20by%20Computer%2C%20bin(TimeGenerated%2C%201h)%0A%7C%20sort%20by%20TimeGenerated%20desc%3C%2FPRE%3E%0A%3CP%3Eand%20got%207%20bins%20of%20results.%20Since%20ran%20the%20query%20around%2015%3A10%3A00%20UTC%20and%20considering%20the%206-hour%20selected%20time%20range%2C%20the%20results%20I%20got%20spread%20between%20approximately%2009%3A10%3A00%20and%2015%3A10%3A00.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CU%3ENote%20that%3A%3C%2FU%3E%3C%2FP%3E%0A%3CP%3E%3CU%3E1.%20%22bin()%22%20creates%20bins%20that%20start%20at%20a%20round%20hour%3C%2FU%3E%3C%2FP%3E%0A%3CP%3E%3CU%3E2.%20The%20time%20shown%20in%20the%20results%20is%20the%20starting%20time%20of%20each%20bin%2C%20not%20its%20end%20time.%3C%2FU%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20got%20these%20bins%3A%3C%2FP%3E%0A%3CP%3E09%3A00%3A00%20(which%20shows%20average%20of%20records%20timed%20between%2009%3A00%3A00%20and%2009%3A59%3A59)%3C%2FP%3E%0A%3CP%3E10%3A00%3A00%20(average%20of%20records%20timed%20between%2010%3A00%3A00%20and%2010%3A59%3A59)%3C%2FP%3E%0A%3CP%3Eand%20so%20on%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20524px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F28880i664A4F1FBA840BC9%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22avg_bin.png%22%20title%3D%22avg_bin.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CU%3EBut%20I%20wanted%20to%20get%20bins%20that%20don't%20start%20at%20a%20round%20hour%2C%20but%20instead%20align%20with%20a%20fixed%20point%20in%20time.%20To%20do%20that%20I%20used%20%22bin_at%22.%3C%2FU%3E%26nbsp%3BThe%20fixed%20point%20I%20chose%20to%20use%20is%20the%20time%20now.%20This%20means%20that%20since%20I%20ran%20the%20query%20at%2015%3A13%3A40%2C%20one%20of%20the%20bins%20should%20align%20(start%20or%20end)%20at%20exactly%20that%20time%2C%20and%20the%20others%20should%20align%20around%20it%2C%20according%20to%20the%20bin-size%20I%20set%20(in%20this%20case%201-hour%20bins).%20This%20is%20the%20query%20syntax%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EPerf%0A%7C%20where%20ObjectName%20%3D%3D%20%22Processor%22%20and%20CounterName%20%3D%3D%20%22%25%20Processor%20Time%22%20and%20InstanceName%20%3D%3D%20%22_Total%22%20and%20Computer%20%3D%3D%20%22ContosoWeb%22%20%0A%7C%20summarize%20avg(CounterValue)%20by%20Computer%2C%20bin_at(TimeGenerated%2C%201h%2C%20now())%0A%7C%20extend%20time_now%20%3D%20now()%20%0A%7C%20sort%20by%20TimeGenerated%20desc%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnd%20as%20you%20see%20the%20bins%20indeed%20show%20start%20and%20end%20at%20xx%3A13%3A40%20of%20each%20hour%2C%20in%20the%206-hour%20time%20range%20I%20applied%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20725px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F28881iDC124F3B1E39F26A%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22avg_bin_at.png%22%20title%3D%22avg_bin_at.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ESince%20I%20don't%20have%20any%20results%20that%20are%20timed%20past%20my%20fixed%20point%20-%26nbsp%3B%22now()%22%20-%20I%20don't%20have%20a%20bin%20that%20starts%20at%2015%3A13%3A40%2C%20yet.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20hope%20this%20helps.%20If%20there%20are%20still%20doubts%2C%20please%20let%20me%20know.%3C%2FP%3E%0A%3CP%3ENoa%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-160269%22%20slang%3D%22en-US%22%3ERe%3A%20Trying%20to%20understand%20bin_at%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-160269%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20fixed%20point%20value%20determines%20fixed%20offset%20from%20the%20binning%20that%20would%20occur%20using%20the%20bin()%20function%20without%20the%20third%20parameter.%20So%20for%20example%2C%20if%20you%20run%20the%20following%20query%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22view-line%22%3E%3CSPAN%3E%3CSPAN%20class%3D%22mtk1%20selectQueryDecoration%22%3EHeartbeat%26nbsp%3B%7C%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22mtk6%20selectQueryDecoration%22%3Esummarize%3C%2FSPAN%3E%3CSPAN%20class%3D%22mtk1%20selectQueryDecoration%22%3E%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22mtk6%20selectQueryDecoration%22%3Ecount%3C%2FSPAN%3E%3CSPAN%20class%3D%22mtk1%20selectQueryDecoration%22%3E()%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22mtk6%20selectQueryDecoration%22%3Eby%3C%2FSPAN%3E%3CSPAN%20class%3D%22mtk1%20selectQueryDecoration%22%3E%26nbsp%3Bbin(TimeGenerated%2C%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22mtk7%20selectQueryDecoration%22%3E12%3C%2FSPAN%3E%3CSPAN%20class%3D%22mtk1%20selectQueryDecoration%22%3Eh)%3C%2FSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%20class%3D%22view-line%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20class%3D%22view-line%22%3E%3CSPAN%3EThis%20returns%20rows%20with%20the%20bin%20Timestamp%20and%20the%20summarized%20count.%20For%20me%20this%20defaults%20to%20bins%20starting%20at%20midnight%20and%20midday.%20If%20instead%20I%20wanted%20to%20look%20at%20bins%20starting%20at%205am%2C%20I%20could%20use%20the%20following%20query%3A%26nbsp%3B%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%20class%3D%22view-line%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20class%3D%22view-line%22%3E%0A%3CDIV%3E%0A%3CDIV%3E%3CSPAN%3EHeartbeat%20%7C%20%3C%2FSPAN%3E%3CSPAN%3Esummarize%3C%2FSPAN%3E%3CSPAN%3Ecount%3C%2FSPAN%3E%3CSPAN%3E()%20%3C%2FSPAN%3E%3CSPAN%3Eby%3C%2FSPAN%3E%3CSPAN%3Ebin_at(TimeGenerated%2C%20%3C%2FSPAN%3E%3CSPAN%3E12%3C%2FSPAN%3E%3CSPAN%3Eh%2C%20%3C%2FSPAN%3E%3CSPAN%3Edatetime%3C%2FSPAN%3E%3CSPAN%3E(%3C%2FSPAN%3E%3CSPAN%3E%225%3A00%22%3C%2FSPAN%3E%3CSPAN%3E))%3C%2FSPAN%3E%20%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3ESo%20this%20would%20give%26nbsp%3B12h%20bins%2C%20but%20ensure%20that%20the%20bins%20align%20to%205am%20and%205pm.%20Similarly%2C%20we%20could%20choose%20a%20date%20we%20know%20is%20a%20Sunday%20and%20choose%20bin%20size%207d%20to%20align%20to%20weeks%20starting%20on%20Sunday.%26nbsp%3B%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3EHope%20this%20helps!%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E
Contributor

Hi all

I know this is a silly question but i'm struggling to understand how and where to use bin and bin_at
I've read the docs https://docs.loganalytics.io/docs/Language-Reference/Scalar-functions/bin_at()

but I don't understand the FixedPoint value. What and how should I use it?

Bin is used to round values, correct?

But why and how to use bin_at?

Best regards

2 Replies

The fixed point value determines fixed offset from the binning that would occur using the bin() function without the third parameter. So for example, if you run the following query: 

 

Heartbeat | summarize count() by bin(TimeGenerated, 12h) 
 
This returns rows with the bin Timestamp and the summarized count. For me this defaults to bins starting at midnight and midday. If instead I wanted to look at bins starting at 5am, I could use the following query: 
 
Heartbeat | summarize count() by bin_at(TimeGenerated, 12h, datetime("5:00"))
 
So this would give 12h bins, but ensure that the bins align to 5am and 5pm. Similarly, we could choose a date we know is a Sunday and choose bin size 7d to align to weeks starting on Sunday. 
 
Hope this helps! 
best response confirmed by Dante Nahuel Ciai (Contributor)
Solution

Hi Dante,

 

This is not a silly question at all. @Stanislav Zhelyazkov noted your question and also brought to my attention how confusing the behavior is, so I'd like to explain how it actually works, and will also push to update the documentation of it.

 

First, I ran this query to get the latest CPU report on a Computer named "ContosoWeb":

Perf
| where ObjectName == "Processor" and CounterName == "% Processor Time" and InstanceName == "_Total" and Computer == "ContosoWeb" 
| summarize arg_max(TimeGenerated, *)

The results showed the latest records is from 15:03:57.arg_max.png

 

I wanted to calculate the average CPU usage per hour, over the last 6 hours (not shown in this query, selected in the UI), so I used bin:

 

Perf
| where ObjectName == "Processor" and CounterName == "% Processor Time" and InstanceName == "_Total" and Computer == "ContosoWeb" 
| summarize AVGCPU = avg(CounterValue) by Computer, bin(TimeGenerated, 1h)
| sort by TimeGenerated desc

and got 7 bins of results. Since ran the query around 15:10:00 UTC and considering the 6-hour selected time range, the results I got spread between approximately 09:10:00 and 15:10:00.

 

Note that:

1. "bin()" creates bins that start at a round hour

2. The time shown in the results is the starting time of each bin, not its end time.

 

I got these bins:

09:00:00 (which shows average of records timed between 09:00:00 and 09:59:59)

10:00:00 (average of records timed between 10:00:00 and 10:59:59)

and so on:

avg_bin.png

 

But I wanted to get bins that don't start at a round hour, but instead align with a fixed point in time. To do that I used "bin_at". The fixed point I chose to use is the time now. This means that since I ran the query at 15:13:40, one of the bins should align (start or end) at exactly that time, and the others should align around it, according to the bin-size I set (in this case 1-hour bins). This is the query syntax:

 

Perf
| where ObjectName == "Processor" and CounterName == "% Processor Time" and InstanceName == "_Total" and Computer == "ContosoWeb" 
| summarize avg(CounterValue) by Computer, bin_at(TimeGenerated, 1h, now())
| extend time_now = now() 
| sort by TimeGenerated desc

 

And as you see the bins indeed show start and end at xx:13:40 of each hour, in the 6-hour time range I applied:

avg_bin_at.png

Since I don't have any results that are timed past my fixed point - "now()" - I don't have a bin that starts at 15:13:40, yet.

 

I hope this helps. If there are still doubts, please let me know.

Noa