How To Use the MID Function in Power BI
What Is MID?
MID is a DAX function within Power BI that allows you to return a string of characters from the middle of a text string using the starting position and length specified.
MID is very similar to the LEFT and RIGHT DAX functions.
Syntax
The syntax for the MID function is below.
=MID(<text>, <start_num>, <num_chars>)
Parameters
Term | Definition |
text | The text string from which you want to extract the characters, or a column that contains text. |
start_num | The position of the first character you want to extract. Positions start at 1. |
num_chars | The number of characters to return. |
Using the MID Function in Power BI
We know what the MID function in power bi does, the syntax for the function, and what the parameters need, but how do we use this DAX function? Let’s find out!
I have a simple dataset that consists of the following columns: ‘Month’, ‘Region’ and ‘Revenue’.
- In your dataset, create a new column.
- Following the MID syntax outlined above, enter your own MID calculation on a column of your choice. For this article, I will be using the ‘Region’ column.
- I have used the below calculation to get the first 2 characters after the second character in the text string.
- Middle 2 = MID(‘RLS Data'[Region], 2, 2)
- This calculation takes the ‘Region’ string which contains multiple regions that are represented as text, finds the second character in the string, then takes 2 characters from the second character.
- The above calculation will then create a column with the below text in.
Notice how the newly created column contains the middle 2 characters of the region strings? ‘East’ becomes ‘as’ and ‘West’ becomes ‘es’.
Finishing Off
The MID function is an extremely powerful and useful DAX function. It provides functionality to extract a specified number of characters within the given string.
Why not challenge yourself and expand on this article? You know how to use the MID function now, but why not challenge yourself and see what you’re able to do with it? Why not take 5 characters after the first space in a string?
To learn more, please visit Learn DAX