Time range (for last month) in Kusto Query language in Logs Analytics Workspace

Varun Kalia 0 Reputation points
2025-02-13T05:54:00.9966667+00:00

Hi,

We use Log Analytics Workspace to collect logs for our customer tenants under a resource hosted in Azure. Previously we would select Time Range feature to select the hits per tenant for the last calendar month and I was looking to set it in the query.

When I set it in the query with the following syntax, the result is double of what I received while using Time Range-

let lastmonth = getmonth(datetime(now)) -1;
let monthEnd = endofmonth(datetime(now),-1); 
AuditLogs
| where TimeGenerated >= make_datetime(lastmonth) and TimeGenerated <= monthEnd

Could someone please review this and let me know what could be the issue?

Appreciate your assistance.

Kind regards.

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,484 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Deepanshu katara 14,330 Reputation points MVP
    2025-02-13T06:41:30.0633333+00:00

    Hello Varun , Welcome to MS Q&A

    When using Log Analytics Workspace, if your query returns double the results compared to using the Time Range feature, it might be due to how the query is structured. If the time filter is applied after a union of several tables, the query may scan all the data instead of just the specified time range, leading to more records than expected.

    To ensure the query only processes the relevant records, apply the time filter within each subquery before performing the union. This way, the query will only consider data from the specified time frame, preventing the retrieval of additional records.

    For more detailed guidance, you can refer to the following resources:

    Please let us know if any further questions

    Kindly accept if it helps

    Thanks
    Deepanshu

    0 comments No comments

  2. Vinod Pittala 330 Reputation points Microsoft Vendor
    2025-02-13T23:47:49.6566667+00:00

    Hello Varun Kalia,

    Try out running the below Kusto query.

    It will retrieve audit logs from the beginning of the previous month to the start of the current month.

    AuditLogs
    | where TimeGenerated >= startofmonth(datetime_add('month', -1, now())) and TimeGenerated < startofmonth(now())
    
    
    

    Hope this helps!

    Please reply if there are any challenges.

    Please do not forget to "Accept the answer” and “upvote it” wherever the information provided helps you, this can be beneficial to other community members.it would be greatly appreciated and helpful to others.

    Thanks


  3. Madugula Jahnavi 0 Reputation points Microsoft Vendor
    2025-02-24T10:59:53.2066667+00:00

    Hello Varun Kalia,

    You can use the below KQL query implemented to retrieve the logs during required time stamp. (Eg: For a month)
    Reference Kusto function: make_datetime()

    let Numberofthelastmonth = getmonth(datetime(now)) - 1;
    let lastmonth = iff(Numberofthelastmonth == 0, 12, Numberofthelastmonth);
    let year = getyear(datetime(now)) - iff(lastmonth == 12, 1, 0);
    let Startdate = make_datetime(year, lastmonth, 01);
    let Enddate = endofmonth(Startdate);
    AzureActivity
    | where TimeGenerated between(Startdate .. Enddate)
    

    kqlimage

    Note: In the above query, I used the "AzureActivity" table for better understanding, as there aren't many logs available in the Audit Logs table. The output should behave similarly in both cases.

    Refer Blog by Stephane Lapointe for more detailed information on the similar requirement.

    Alternatively, you can manually hardcode the start date and end date values of a specific month and directly retrieve the logs in the below way.

    Reference Kusto operator: between

    AuditLogs 
    | where TimeGenerated between(startofday(datetime(2025-02-01)) .. endofday(datetime(2025-02-28)))
    

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.