Thread

Power Query M - Classify Shape Color Function

This Power Query M function, classifyShapeColor, categorizes shapes based on shape and color values provided, allowing users to create a new column in Power Query Editor for classification within a table.

Empty image or helper icon

Power Query M - Classify Shape Color Function

Description

This Power Query M function, classifyShapeColor, categorizes shapes based on shape and color values provided, allowing users to create a new column in Power Query Editor for classification within a table.

Power Query M - Classify Shape Color Function

Tool: Code Generator

Created: 05/01/2024

Prompt

if formaTrimmed = "star" and culoareTrimmed = "purple" then "Purple Star"
            else if formaTrimmed = "star" and culoareTrimmed = "black" then "Black Star"
            else if formaTrimmed = "square" and culoareTrimmed = "green" then "Green Square"
            else if formaTrimmed = "circle" and culoareTrimmed = "blue" then "Blue Circle"
            else "null"
/*
Function: classifyShapeColor

This function takes two parameters formaTrimmed and culoareTrimmed and returns a corresponding classification based on certain conditions.

Parameters:
formaTrimmed (text): The shape value trimmed.
culoareTrimmed (text): The color value trimmed.

Returns:
text: The classification based on the conditions provided.

*/

classifyShapeColor = (formaTrimmed as text, culoareTrimmed as text) =>
    let
        classification = 
            if formaTrimmed = "star" and culoareTrimmed = "purple" then "Purple Star"
            else if formaTrimmed = "star" and culoareTrimmed = "black" then "Black Star"
            else if formaTrimmed = "square" and culoareTrimmed = "green" then "Green Square"
            else if formaTrimmed = "circle" and culoareTrimmed = "blue" then "Blue Circle"
            else "null"
    in
        classification

Usage Example:

Assuming you have a table named "Shapes" with columns "formaTrimmed" and "culoareTrimmed", you can apply this function to create a new column "Classification" using Power Query Editor:

  1. Go to Power Query Editor.
  2. Select the table "Shapes".
  3. Click on "Add Column" -> "Custom Column".
  4. Enter a new column name, e.g., "Classification".
  5. Input the formula classifyShapeColor([formaTrimmed], [culoareTrimmed]).
  6. Click "OK" to create the new column with the classification results based on the conditions defined in the function.