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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
webportal
Impactful Individual
Impactful Individual

Extract data from JSON field

I'm using Power BI desktop to connect to a MySQL database.

One of the fields contains data with the following structure:

 

a:1:{s:3:"IVA";O:8:"stdClass":3:{s:11:"tax_namekey";s:3:"IVA";s:8:"tax_rate";s:7:"0.23000";s:10:"tax_amount";d:25.07000000000000028421709430404007434844970703125;}}

 

I need to transform the data in a way that allows the extraction of the value of the tax amount. That is, I need to transform this column to: 25.07.

 

How can I do this? I tried splitting the column by semicolon, but since not all the columns have the same number of semicolons it didn't work.

 

Thanks in advance!

1 ACCEPTED SOLUTION

@webportal

 

Then add a IF statement.

 

let
    Source = Table.FromRows({{1, "a:1:{s:3:""IVA"";O:8:""stdClass"":3:{s:11:""tax_namekey"";s:3:""IVA"";s:8:""tax_rate"";s:7:""0.23000"";s:10:""tax_amount"";d:25.07000000000000028421709430404007434844970703125;}}"},{1, "a:1:{s:3:""IVA"";O:8:""stdClass"":3:{s:11:""tax_namekey"";s:3:""IVA"";s:8:""tax_rate"";s:7:""0.23000"";s:10:""tax_amount"";"}},{"id", "text"}),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each if Text.PositionOf([text],"d:")>0 then Number.FromText(Text.Range([text],Text.PositionOf([text],"d:")+2,5)) else 0)  

in
    #"Added Custom"

View solution in original post

12 REPLIES 12
Eric_Zhang
Microsoft Employee
Microsoft Employee

@webportal

 

The subject is a little misleading as that string is not in a valid json format, is that a typo? If JSON, you can extract value in MySQL end, check Searching and Modifying JSON Values.

 

Anyway, if the prefix "d:" is fixed, use a sub-string funtion, namely Text.Range in power query to extract that value.

 

let
    Source = Table.FromRows({{1, "a:1:{s:3:""IVA"";O:8:""stdClass"":3:{s:11:""tax_namekey"";s:3:""IVA"";s:8:""tax_rate"";s:7:""0.23000"";s:10:""tax_amount"";d:25.07000000000000028421709430404007434844970703125;}}"}},{"id", "text"}),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each Text.Range([text],Text.PositionOf([text],"d:")+2,5))  

in
    #"Added Custom"

 

Thank you @Eric_Zhang

 

The query worked, but only for that specific row. All other rows are gone.

 

🙂

@webportal

 

So can you confirm that string is a valid JSON?

 

If not and all the rows in your case don't have certain patterns(eg: a "d:" prefix before the number), then I think there's almost no way to achieve your requirement.

 

Can you be more specific about you scenario, what are the rest rows like?

OK, I see.

 

Well, some rows have that pattern while others have not.

They can either be:

a:1:{s:10:"VAT Exempt";O:8:"stdClass":3:{s:11:"tax_namekey";s:10:"VAT Exempt";s:8:"tax_rate";s:7:"0.00000";s:10:"tax_amount";i:0;}}

 

Or:

a:1:{s:3:"IVA";O:8:"stdClass":3:{s:11:"tax_namekey";s:3:"IVA";s:8:"tax_rate";s:7:"0.23000";s:10:"tax_amount";d:12.6500000000000003552713678800500929355621337890625;}}

 

If it's impossible to grab the tax amount, it's ok, I understand this is a complicated structure.


Thanks for your help!


@webportal wrote:

OK, I see.

 

Well, some rows have that pattern while others have not.

They can either be:

a:1:{s:10:"VAT Exempt";O:8:"stdClass":3:{s:11:"tax_namekey";s:10:"VAT Exempt";s:8:"tax_rate";s:7:"0.00000";s:10:"tax_amount";i:0;}}

 

Or:

a:1:{s:3:"IVA";O:8:"stdClass":3:{s:11:"tax_namekey";s:3:"IVA";s:8:"tax_rate";s:7:"0.23000";s:10:"tax_amount";d:12.6500000000000003552713678800500929355621337890625;}}

 

If it's impossible to grab the tax amount, it's ok, I understand this is a complicated structure.


Thanks for your help!


 

The solution in my previous reply should work with the latter pattern in your reply. What value is expected from the former case?

In the former case, there's no tax, so the field should be zero.

@webportal

 

Then add a IF statement.

 

let
    Source = Table.FromRows({{1, "a:1:{s:3:""IVA"";O:8:""stdClass"":3:{s:11:""tax_namekey"";s:3:""IVA"";s:8:""tax_rate"";s:7:""0.23000"";s:10:""tax_amount"";d:25.07000000000000028421709430404007434844970703125;}}"},{1, "a:1:{s:3:""IVA"";O:8:""stdClass"":3:{s:11:""tax_namekey"";s:3:""IVA"";s:8:""tax_rate"";s:7:""0.23000"";s:10:""tax_amount"";"}},{"id", "text"}),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each if Text.PositionOf([text],"d:")>0 then Number.FromText(Text.Range([text],Text.PositionOf([text],"d:")+2,5)) else 0)  

in
    #"Added Custom"
donsvensen
Skilled Sharer
Skilled Sharer

Hi,

 

One way is to use the Text functions in Power Query

 

 

//find the position

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.PositionOf([JSON], "amount")),

//use the position and offset it

    #"Added Custom1" = Table.AddColumn(#"Added Custom", "d", each Text.Range([JSON], [Custom]+10, 5))

 

this requires a fixed length after the text "amount"

 

/Erik

 

Hi,

 

Thanks a lot for helping, but nothing happens after inserting that code in the advanced editor.

Hi

 

Can you share the lines of your query statement just before you want to calculate the taxamount ?

 

/Erik

Hello,

 

Here they are:

let
    Source = MySQL.Database("host.domain.com", "database", [ReturnSingleDatabase=true]),
    tablexxx = Source{[Schema="database",Item="tablexxx"]}[Data]
in
    database_tablexxx

Hi again,

 

You should modify the first line to this then

 

    #"Added Custom" = Table.AddColumn(tablexxx, "Custom", each Text.PositionOf([JSON], "amount")),

 

and the bold+italic column name should refer to the field in your table

 

/Erik

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

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.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

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