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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
ADSL
Post Prodigy
Post Prodigy

# Order & # NO Order by time range condition

Hi BI Community Team,

 

I have a table called "Visit Summary by Sales Rep" that it contains the "Time-In" & "Time-Out" when they visit the customer.

 

> I need help in categorizing the time into groups - early morning, morning, lunch, afternoon, evening & late evening:

 

If time is from 1:00:00 am to 7:59:59 am = early morning

If time is from 8:00:00 am to 11:59:59 am = morning

If time is from 12:00:00 pm to 13:29:59 pm = lunch

If time is from 13:30:00 pm to 17:29:59 pm = afternoon

If time is from 17:30:00 pm to 20:59:59 pm = evening

If time is from 21:00:00 pm to 23:59:59 pm = late evening

 

> In these groups, we need to know how many # order, # NO order & customer visited

 

Any suggestion/advise? Please kindly help.

 

Thanks and Regards,

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @ADSL ,

 

I suggest you to create a [Time Group] column in your VISIT_SUMM table.

Time Group = 
IF (
    VISIT_SUMM[TIME_IN] = BLANK ()
        || VISIT_SUMM[TIME_OUT] = BLANK (),
    BLANK (),
    SWITCH (
        TRUE (),
        VISIT_SUMM[TIME_IN] >= TIME ( 1, 0, 0 )
            && VISIT_SUMM[TIME_OUT] <= TIME ( 7, 59, 59 ), "Early Morning (1:00:00 am - 7:59:59 am)",
        VISIT_SUMM[TIME_IN] >= TIME ( 8, 00, 00 )
            && VISIT_SUMM[TIME_OUT] <= TIME ( 11, 59, 59 ), "Morning (8:00:00 am - 11:59:59 am)",
        VISIT_SUMM[TIME_IN] >= TIME ( 12, 00, 00 )
            && VISIT_SUMM[TIME_OUT] <= TIME ( 13, 29, 59 ), "Lunch (12:00:00 pm - 13:29:59 pm)",
        VISIT_SUMM[TIME_IN] >= TIME ( 13, 30, 00 )
            && VISIT_SUMM[TIME_OUT] <= TIME ( 17, 29, 59 ), "Afternoon (13:30:00 pm - 17:29:59 pm)",
        VISIT_SUMM[TIME_IN] >= TIME ( 17, 30, 00 )
            && VISIT_SUMM[TIME_OUT] <= TIME ( 20, 59, 59 ), "Evening (17:30:00 pm - 20:59:59 pm)",
        VISIT_SUMM[TIME_IN] >= TIME ( 21, 00, 00 )
            && VISIT_SUMM[TIME_OUT] <= TIME ( 23, 59, 59 ), "Late evening (21:00:00 pm - 23:59:59 pm)"
    )
)

Then create a calculated table.

DimTimeGourp =
DATATABLE (
    "TimeGroup", STRING,
    "GroupSort", INTEGER,
    "TimeIn", STRING,
    "TimeOut", STRING,
    {
        { "Early Morning (1:00:00 am - 7:59:59 am)", 1, "1:00:00 am", "7:59:59 am" },
        { "Morning (8:00:00 am - 11:59:59 am)", 2, "8:00:00 am", "11:59:59 am" },
        { "Lunch (12:00:00 pm - 13:29:59 pm)", 3, "12:00:00 pm", "13:29:59 pm" },
        { "Afternoon (13:30:00 pm - 17:29:59 pm)", 4, "13:30:00 pm", "17:29:59 pm" },
        { "Evening (17:30:00 pm - 20:59:59 pm)", 5, "17:30:00 pm", "20:59:59 pm" },
        { "Late evening (21:00:00 pm - 23:59:59 pm)", 6, "21:00:00 pm", "23:59:59 pm" }
    }
)

Data model:

vrzhoumsft_0-1687762955588.png

Measures:

#Order = 
CALCULATE(COUNT(VISIT_SUMM[SALESREP_CODE]),FILTER(VISIT_SUMM,VISIT_SUMM[ORDER_IND] = "E" && VISIT_SUMM[VISIT_IND] = "V" && VISIT_SUMM[CALL_IND] = "1"))
NO Order = 
CALCULATE(COUNT(VISIT_SUMM[SALESREP_CODE]),FILTER(VISIT_SUMM,VISIT_SUMM[ORDER_IND] = BLANK() && VISIT_SUMM[VISIT_IND] = "V" && VISIT_SUMM[CALL_IND] = "1"))
Customer Visited = 
CALCULATE(COUNT(VISIT_SUMM[SALESREP_CODE]),FILTER(VISIT_SUMM,VISIT_SUMM[VISIT_IND] = "V" && VISIT_SUMM[CALL_IND] = "1"))

Result is as below.

vrzhoumsft_1-1687763028282.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @ADSL ,

 

I suggest you to create a [Time Group] column in your VISIT_SUMM table.

Time Group = 
IF (
    VISIT_SUMM[TIME_IN] = BLANK ()
        || VISIT_SUMM[TIME_OUT] = BLANK (),
    BLANK (),
    SWITCH (
        TRUE (),
        VISIT_SUMM[TIME_IN] >= TIME ( 1, 0, 0 )
            && VISIT_SUMM[TIME_OUT] <= TIME ( 7, 59, 59 ), "Early Morning (1:00:00 am - 7:59:59 am)",
        VISIT_SUMM[TIME_IN] >= TIME ( 8, 00, 00 )
            && VISIT_SUMM[TIME_OUT] <= TIME ( 11, 59, 59 ), "Morning (8:00:00 am - 11:59:59 am)",
        VISIT_SUMM[TIME_IN] >= TIME ( 12, 00, 00 )
            && VISIT_SUMM[TIME_OUT] <= TIME ( 13, 29, 59 ), "Lunch (12:00:00 pm - 13:29:59 pm)",
        VISIT_SUMM[TIME_IN] >= TIME ( 13, 30, 00 )
            && VISIT_SUMM[TIME_OUT] <= TIME ( 17, 29, 59 ), "Afternoon (13:30:00 pm - 17:29:59 pm)",
        VISIT_SUMM[TIME_IN] >= TIME ( 17, 30, 00 )
            && VISIT_SUMM[TIME_OUT] <= TIME ( 20, 59, 59 ), "Evening (17:30:00 pm - 20:59:59 pm)",
        VISIT_SUMM[TIME_IN] >= TIME ( 21, 00, 00 )
            && VISIT_SUMM[TIME_OUT] <= TIME ( 23, 59, 59 ), "Late evening (21:00:00 pm - 23:59:59 pm)"
    )
)

Then create a calculated table.

DimTimeGourp =
DATATABLE (
    "TimeGroup", STRING,
    "GroupSort", INTEGER,
    "TimeIn", STRING,
    "TimeOut", STRING,
    {
        { "Early Morning (1:00:00 am - 7:59:59 am)", 1, "1:00:00 am", "7:59:59 am" },
        { "Morning (8:00:00 am - 11:59:59 am)", 2, "8:00:00 am", "11:59:59 am" },
        { "Lunch (12:00:00 pm - 13:29:59 pm)", 3, "12:00:00 pm", "13:29:59 pm" },
        { "Afternoon (13:30:00 pm - 17:29:59 pm)", 4, "13:30:00 pm", "17:29:59 pm" },
        { "Evening (17:30:00 pm - 20:59:59 pm)", 5, "17:30:00 pm", "20:59:59 pm" },
        { "Late evening (21:00:00 pm - 23:59:59 pm)", 6, "21:00:00 pm", "23:59:59 pm" }
    }
)

Data model:

vrzhoumsft_0-1687762955588.png

Measures:

#Order = 
CALCULATE(COUNT(VISIT_SUMM[SALESREP_CODE]),FILTER(VISIT_SUMM,VISIT_SUMM[ORDER_IND] = "E" && VISIT_SUMM[VISIT_IND] = "V" && VISIT_SUMM[CALL_IND] = "1"))
NO Order = 
CALCULATE(COUNT(VISIT_SUMM[SALESREP_CODE]),FILTER(VISIT_SUMM,VISIT_SUMM[ORDER_IND] = BLANK() && VISIT_SUMM[VISIT_IND] = "V" && VISIT_SUMM[CALL_IND] = "1"))
Customer Visited = 
CALCULATE(COUNT(VISIT_SUMM[SALESREP_CODE]),FILTER(VISIT_SUMM,VISIT_SUMM[VISIT_IND] = "V" && VISIT_SUMM[CALL_IND] = "1"))

Result is as below.

vrzhoumsft_1-1687763028282.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @Anonymous ,

Thank you very much for your helpful feedback.

 

Best Regards,

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.