How do I create a column that extracts the last numeric value from a string

0 votes

How do I create a column that extracts the last numeric value from a string?
I need to create a new column in Power BI that extracts the last numeric value from a text string. The strings may contain a mix of letters, numbers, and special characters, and only the final numeric portion is needed. What is the most efficient method using Power Query M functions or DAX to isolate and extract this last numeric value accurately?

4 days ago in Power BI by Evanjalin
• 25,690 points
35 views

1 answer to this question.

0 votes

In Power BI, Power Query (M language) is the best approach to extract the last numeric value from a text string, besides being flexible. This is the best procedure if the string is complex.

Power Query Solution (preferred) 
He will need to follow the steps in the Power Query Editor:

Transform Data. Select the Column by clicking Add Column> Custom Column while selected.

let
    text = [YourColumnName],
    numbers = List.Select(Text.ToList(text), each Value.Is(Value.FromText(_), type number)),
    reversedText = Text.Reverse(text),
    matches = Text.RegexReplace(reversedText, "[^\d]+", "|"),
    lastNumberReversed = List.Last(Text.Split(matches, "|")),
    lastNumber = Text.Reverse(lastNumberReversed)
in
    lastNumber

Replace [YourColumnName] with the actual name of your column.

Explanation

  • Text.Reverse(text) ensures we start from the end.

  • Text.RegexReplace(..., "[^\d]+", "|") replaces all non-digit sequences with a delimiter.

  • List.Last(...) gets the first numeric group from the end.

  • Text.Reverse(...) flips it back to the correct order.

answered 4 days ago by anonymous
• 25,690 points

Related Questions In Power BI

0 votes
1 answer

In DAX, how do I RETURN the sum of a calculated column from a DAX Table Variable (created via ADDCOLUMN)?

You can access column variables of previously ...READ MORE

answered Oct 8, 2020 in Power BI by Gitika
• 65,770 points
4,769 views
0 votes
1 answer

How do I create a dynamic column in Power Query that calculates based on multiple conditions?

To create a dynamic column in Power ...READ MORE

answered 4 days ago in Power BI by anonymous
• 25,690 points
39 views
0 votes
1 answer

How can I get a column value from previous row in Power Query?

Hi Sindhu, add this line as your ...READ MORE

answered Mar 18, 2019 in Power BI by Cherukuri
• 33,050 points
8,582 views
0 votes
0 answers
0 votes
1 answer

Displaying Table Schema using Power BI with Azure IoT Hub

Answering your first question, Event Hubs are ...READ MORE

answered Aug 1, 2018 in IoT (Internet of Things) by nirvana
• 3,090 points
1,598 views
+1 vote
1 answer

Unable to install connector for Power Bi and PostgreSQL

I think the problem is not at ...READ MORE

answered Aug 22, 2018 in Power BI by nirvana
• 3,090 points
2,936 views
+2 votes
2 answers

Migrate power bi collection to power bi embedded

I agree with Kalgi, this method is ...READ MORE

answered Oct 11, 2018 in Power BI by Hannah
• 18,520 points
1,721 views
+1 vote
1 answer

Connect power bi desktop to dataset and create custom reports

Open power bi report nd sign in ...READ MORE

answered Oct 10, 2023 in Power BI by Monika kale

edited Mar 5 1,884 views
0 votes
1 answer
0 votes
1 answer

How do I calculate the most frequently occurring value in a column using DAX?

To find the mode in a column, ...READ MORE

answered Apr 1 in Power BI by anonymous
• 25,690 points
69 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP