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

Did you hear? There's a new SQL AI Developer certification (DP-800). Start preparing now and be one of the first to get certified. Register now

Reply
Anonymous
Not applicable

Rank for data

I would like to create a rank by date and time, based on this information in the picture, the result needs to be
10/07/2022 23:00 36065
10/07/2022 22:00 35848

I am using the power query via power query,

FerAlvarenga_1-1665712066330.png

 

1 ACCEPTED SOLUTION

Ah, sorry, did not quite understand the requirements reflected from your desired output. It looks like you want to get the breakdown by hour and get the earliest integer from [pedidos_vtex], right?

 

The approach to take is to add a column that provides the datetime rounded down to start of the hour, group on that column, and get the minimum pedidos_vtex value. The following M code can achieve this:

 

 

let
    Source = SampleData,
    AddByHour = 
    Table.AddColumn(
        Source, 
        "Datetime By Hour", 
        each Time.StartOfHour([data_consul]), 
        type datetime
    ),
    GroupByHourMinVtex = 
    Table.Group(
        AddByHour, 
        {"Datetime By Hour"}, 
        {{
            "pedidos_vtex_FirstInHour", 
            each List.Min([pedidos_vtex]), 
            type nullable Int64.Type
        }}
    )
in
    GroupByHourMinVtex

 

 

The above code is working with the following sample data:

data_consul pedidos_vtex
10/7/2022 11:52:52 PM 36196
10/7/2022 11:47:34 PM 36184
10/7/2022 11:04:46 PM 36065
10/7/2022 10:59:26 PM 36054
10/7/2022 10:38:01 PM 35979
10/7/2022 10:00:42 PM 35848
10/7/2022 9:55:31 PM 35151
10/7/2022 9:40:39 PM 35148
10/7/2022 9:00:00 PM 35145
10/7/2022 8:59:59 PM 35140
10/7/2022 8:23:35 PM 35138
10/7/2022 8:10:10 PM 35132

 

And produces the following output:

Datetime By Hour pedidos_vtex_FirstInHour
10/7/2022 11:00:00 PM 36065
10/7/2022 10:00:00 PM 35848
10/7/2022 9:00:00 PM 35145
10/7/2022 8:00:00 PM 35132

 

(Edit: simplified M, specifically AddByHour - Time.StartOfHour actually can take time, datetime, or datetimezone as an input, so using Time.From and adding to Date.From was unnecessary)

View solution in original post

4 REPLIES 4
MarkLaf
Super User
Super User

In Power Query, just sort the table so it is in your desired order, then add an index column.

MarkLaf_0-1665717232166.png

 

Anonymous
Not applicable

By inserting this index, I make a simple rank, I need the breakdown of the rank by hour...,

Ah, sorry, did not quite understand the requirements reflected from your desired output. It looks like you want to get the breakdown by hour and get the earliest integer from [pedidos_vtex], right?

 

The approach to take is to add a column that provides the datetime rounded down to start of the hour, group on that column, and get the minimum pedidos_vtex value. The following M code can achieve this:

 

 

let
    Source = SampleData,
    AddByHour = 
    Table.AddColumn(
        Source, 
        "Datetime By Hour", 
        each Time.StartOfHour([data_consul]), 
        type datetime
    ),
    GroupByHourMinVtex = 
    Table.Group(
        AddByHour, 
        {"Datetime By Hour"}, 
        {{
            "pedidos_vtex_FirstInHour", 
            each List.Min([pedidos_vtex]), 
            type nullable Int64.Type
        }}
    )
in
    GroupByHourMinVtex

 

 

The above code is working with the following sample data:

data_consul pedidos_vtex
10/7/2022 11:52:52 PM 36196
10/7/2022 11:47:34 PM 36184
10/7/2022 11:04:46 PM 36065
10/7/2022 10:59:26 PM 36054
10/7/2022 10:38:01 PM 35979
10/7/2022 10:00:42 PM 35848
10/7/2022 9:55:31 PM 35151
10/7/2022 9:40:39 PM 35148
10/7/2022 9:00:00 PM 35145
10/7/2022 8:59:59 PM 35140
10/7/2022 8:23:35 PM 35138
10/7/2022 8:10:10 PM 35132

 

And produces the following output:

Datetime By Hour pedidos_vtex_FirstInHour
10/7/2022 11:00:00 PM 36065
10/7/2022 10:00:00 PM 35848
10/7/2022 9:00:00 PM 35145
10/7/2022 8:00:00 PM 35132

 

(Edit: simplified M, specifically AddByHour - Time.StartOfHour actually can take time, datetime, or datetimezone as an input, so using Time.From and adding to Date.From was unnecessary)

Anonymous
Not applicable

Thanks a lot, it worked!

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.