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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Measure help

Hi Power Users,

I'm new to power bi and trying to migrate formulas from tableau. 

IF DATEPART('hour',[Date of entry]) = 12 THEN '12 PM'

ELSEIF DATEPART('hour',[Date of entry]) = 13 THEN '1 PM'

ELSEIF DATEPART('hour',[Date of entry]) = 14 THEN '2 PM'

ELSEIF DATEPART('hour',[Date of entry]) = 15 THEN '3 PM'

ELSEIF DATEPART('hour',[Date of entry]) = 16 THEN '4 PM'

ELSEIF DATEPART('hour',[Date of entry]) = 17 THEN '5 PM'

ELSEIF DATEPART('hour',[Date of entry]) = 18 THEN '6 PM'

ELSEIF DATEPART('hour',[Date of entry]) = 19 THEN '7 PM'

ELSEIF DATEPART('hour',[Date of entry]) = 20 THEN '8 PM'

ELSEIF DATEPART('hour',[Date of entry]) = 21 THEN '9 PM'

ELSEIF DATEPART('hour',[Date of entry]) = 22 THEN '10 PM'

ELSEIF DATEPART('hour',[Date of entry]) = 23 THEN '11 PM'

ELSEIF DATEPART('hour',[Date of entry]) = 0 THEN '12 AM'

ELSE STR(DATEPART('hour',[Date of entry])) + ' AM'

END

How can i create above calculation in DAX. i need to use this column in Chart.

 

Hour column in x-axis

 

Please help

5 REPLIES 5
mahoneypat
Microsoft Employee
Microsoft Employee

FYI that there is no need for a nested IF or SWITCH, as you can do this in the query editor or with a DAC calculated column with this formula, where 'Hour'[Hour] should be replaced with your actual Table[Column] in a number format.

 

Column = FORMAT(TIME('Hour'[Hour],0,0), "h AM/PM")
 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


lbendlin
Super User
Super User

Tahreem24
Super User
Super User

@Anonymous ,

You can the SWITCH :

Column = SWITCH(HOUR(Table[Date of Entry]),12,"12PM",1,"1PM",2,"2PM",3,"3PM",HOUR(Table[Date of Entry])&"AM")

So complete the list and at end in else part put HOUR(Table[Date of Entry])&"AM".

 

Don't forget to give thumbs up and accept this as a solution if it helped you!!!

Please take a quick glance at newly created dashboards : Restaurant Management Dashboard , HR Analytics Report , Hotel Management Report, Sales Analysis Report , Fortune 500 Companies Analysis , Revenue Tracking Dashboard
az38
Community Champion
Community Champion

Hi @Anonymous 

create a SWITCH column

Column = 
SWITCH(HOUR([Date of entry]),
12,'12 PM',
13, '1 PM',
....
0, '12 AM',
HOUR([Date of entry]) & ' AM'
)

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn
Anonymous
Not applicable


@az38 wrote:

Hi @Anonymous 

create a SWITCH column

 

Column = 
SWITCH(HOUR([Date of entry]),
12,'12 PM',
13, '1 PM',
....
0, '12 AM',
HOUR([Date of entry]) & ' AM'
)

 


@az38 Thanks for your reply. This is working but when i select the date range from 01/01/2018 to 02/02/2020 the bar visual is showing multiple hours. Example 12 AM repeating many times based on the data. In X-Axis i have Hour and in Y-Axis i have count of ID's. I want to show 12AM,1AM,2AM... Count of ID for each hour. For example if i have 10 12AM's and count for each 10 then i have to show only 1 12AM and add all those counts.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.