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

Try your skills in the Power BI Dataviz World Championship! Round one ends June 26. Join now

Reply
PierreM52
New Member

IF THEN ELSE syntax

Hello all,

 

I've a syntax issue, it returns me "THEN syntax is incorrect"

My code :

IF (HOUR(Production[Debut])>=5
    && HOUR(Production[Debut])<13
    && WEEKDAY(Production[Jour],2)<>5
    THEN "Matin"
    ELSE IF HOUR(Production[Debut])>=13
        && HOUR(Production[Debut])<21
        && WEEKDAY(Production[Jour],2)<>5
        THEN "Après-midi"
        ELSE IF WEEKDAY(Production[Jour],2)<>5
            THEN "Nuit"
            ELSE IF HOUR(Production[Debut])>=5
                && HOUR(Production[Debut])<11
                && WEEKDAY(Production[Jour],2)=5
                THEN "Matin"
                ELSE IF HOUR(Production[Debut])>=11
                    && HOUR(Production[Debut])<17
                    && WEEKDAY(Production[Jour],2)=5
                    THEN "Après-midi"
ELSE "Nuit")

 

Has someone an idea to help me to understand my mistake ? Thanks,

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @PierreM52 

Here's your if then else translated into M for Power Query

I've also created a PBIX file and an Excel Workbook with dummy data showing how this works.

 

#"Added Custom" = Table.AddColumn(PreviousStepName, "Custom", each

if 

(Time.Hour([Debut]) >= 5 and Time.Hour([Debut]) < 13 and Date.DayOfWeek([Jour], Day.Monday) <> 5) then "Matin" 

else if 

(Time.Hour([Debut]) >= 3 and Time.Hour([Debut]) < 21 and Date.DayOfWeek([Jour], Day.Monday) <> 5) then "Après-midi" 

else if 

Date.DayOfWeek([Jour], Day.Monday) <> 5 then "Nuit" 

else if 

(Time.Hour([Debut]) >= 5 and Time.Hour([Debut]) < 11 and Date.DayOfWeek([Jour], Day.Monday) = 5) then "Matin" 

else if 

(Time.Hour([Debut]) >= 11 and Time.Hour([Debut]) < 17 and Date.DayOfWeek([Jour], Day.Monday) = 5) then "Après-midi" 

else "Nuit")

 

Phil


If I answered your question please mark my post as the solution.
If you liked my answer click on the Thumbs Up icon.



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

6 REPLIES 6
PhilipTreacy
Super User
Super User

Hi @PierreM52 

Here's your if then else translated into M for Power Query

I've also created a PBIX file and an Excel Workbook with dummy data showing how this works.

 

#"Added Custom" = Table.AddColumn(PreviousStepName, "Custom", each

if 

(Time.Hour([Debut]) >= 5 and Time.Hour([Debut]) < 13 and Date.DayOfWeek([Jour], Day.Monday) <> 5) then "Matin" 

else if 

(Time.Hour([Debut]) >= 3 and Time.Hour([Debut]) < 21 and Date.DayOfWeek([Jour], Day.Monday) <> 5) then "Après-midi" 

else if 

Date.DayOfWeek([Jour], Day.Monday) <> 5 then "Nuit" 

else if 

(Time.Hour([Debut]) >= 5 and Time.Hour([Debut]) < 11 and Date.DayOfWeek([Jour], Day.Monday) = 5) then "Matin" 

else if 

(Time.Hour([Debut]) >= 11 and Time.Hour([Debut]) < 17 and Date.DayOfWeek([Jour], Day.Monday) = 5) then "Après-midi" 

else "Nuit")

 

Phil


If I answered your question please mark my post as the solution.
If you liked my answer click on the Thumbs Up icon.



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Fowmy
Super User
Super User

@PierreM52 

 

Are you trying this in Power Query or DAX ?

________________________

If my answer was helpful, please consider Accept it as the solution to help the other members find it

Click on the Thumbs-Up icon if you like this reply 🙂

YouTube  LinkedIn

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

@Fowmy 

 

I try this in DAX to create a new column.

 

Thanks,

@PierreM52 

 

The syntax is wrong if it DAX.  You don't enter THEN and ELSE,

 

 

Refer to this article: https://docs.microsoft.com/en-us/dax/if-function-dax

 

________________________

If my answer was helpful, please consider Accept it as the solution to help the other members find it

Click on the Thumbs-Up icon if you like this reply 🙂

YouTube  LinkedIn

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

@Fowmy 

OK thanks, and if I try it in Power Query, what should I change ?

 

Thanks,

@PierreM52 

 

You need use and instead of && also, all lower case

 

Refer to: https://docs.microsoft.com/en-us/powerquery-m/m-spec-conditionals

 

________________________

If my answer was helpful, please consider Accept it as the solution to help the other members find it

Click on the Thumbs-Up icon if you like this reply 🙂

YouTube  LinkedIn

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

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

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.