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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
jjak
Frequent Visitor

Conditional expand of column

Hello! 

 

I have a JSON list which looks like this:

 

 

 

"datasets": [
	{
		"values": [
			{
				"x-original": "Company A",
				"de-DE": "Company A",
				"de": "Company A",
				"en": "Company A"
			},
			
		],
		"id": "1",
		"name": "company",
		"dataType": "STRING",
		"readOnly": false
	},
	{
		"values": [
			"https://link.to.my.website"
		],
		"apiKey": "abc",
		"timeout": 30000,
		"id": "2",
		"name": "website",
		"dataType": "URI",
		"readOnly": false
	}
]

 

 

 

 

I want to load this list into a table with the columns id, name, dataType, readOnly, apiKey, timeout, x-original, de-De, de, en and uri, where the columns are null when the respective value is not present in the dataset list entry. 

 

I can expand the list into a table until the point where I need to expand the values column. But this column contains text and record entries, depending on the type in the column dataType. When the value of dataType is "URI" the values column contains only one text. When it is "STRING" the values column contains a record with four additional values. I do not know how to react on these two different types in one column.

 

I tried to follow the example here and create a custom function to expand the column, but I always end up in errors. Can someone help me how to expand the table correctly? 

 

 

 

 

let CustomExpandTable = (table as table, columnNames as list) =>
    let
        columnType = (columnName as text) as text => Table.SingleRow(Table.SelectRows(Table.Schema(table), each [Name] = columnName))[TypeName],
        expandColumn = (table as table, columnName as text) as table => 
            if columnType(columnName) = "Record.Type" 
            then Table.ExpandRecordColumn(table, columnName, "values", {"x-original", "de-DE", "de", "en"}) 
            else {"values"},
        expandedTable = List.Accumulate(columnNames, table, (state, current) => expandColumn(state, current))
    in
        expandedTable
in
  CustomExpandTable

 

 

 

 

1 ACCEPTED SOLUTION
jjak
Frequent Visitor

Thank you for your answer. Unfortunately I need the record as columns to get the overview I need. This comes with some limitation, but this is currently okay for me.

 

I now solved my problem by using two different queries. Each query just expands the needed data for one of the two dataTypes. Afterwards I just combine these queries and get the desired result. 

View solution in original post

2 REPLIES 2
lbendlin
Super User
Super User

you can use Value.Is to sense that.  I would strongly recommend against trying to expand the record into columns.  This should go into new rows, with a key/value pair of columns.

 

BTW, your JSON was malformed (extraneous comma, missing parentheses).

 

let
    Source = Json.Document( "{""datasets"":[
	{
		""values"": [
			{
				""x-original"": ""Company A"",
				""de-DE"": ""Company A"",
				""de"": ""Company A"",
				""en"": ""Company A""
			}
			
		],
		""id"": ""1"",
		""name"": ""company"",
		""dataType"": ""STRING"",
		""readOnly"": false
	},
	{
		""values"": [
			""https://link.to.my.website""
		],
		""apiKey"": ""abc"",
		""timeout"": 30000,
		""id"": ""2"",
		""name"": ""website"",
		""dataType"": ""URI"",
		""readOnly"": false
	}
]}" ),
    #"Converted to Table" = Record.ToTable(Source),
    #"Expanded Value" = Table.ExpandListColumn(#"Converted to Table", "Value"),
    #"Expanded Value1" = Table.ExpandRecordColumn(#"Expanded Value", "Value", {"values", "id", "name", "dataType", "readOnly", "apiKey", "timeout"}, {"values", "id", "name.1", "dataType", "readOnly", "apiKey", "timeout"}),
    #"Expanded values" = Table.ExpandListColumn(#"Expanded Value1", "values"),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded values",each [values],each if [dataType]="URI" then #table({"Value"},{{[values]}}) else Record.ToTable([values]),Replacer.ReplaceValue,{"values"}),
    #"Expanded values1" = Table.ExpandTableColumn(#"Replaced Value", "values", {"Name", "Value"}, {"Name.2", "Value"})
in
    #"Expanded values1"

 

jjak
Frequent Visitor

Thank you for your answer. Unfortunately I need the record as columns to get the overview I need. This comes with some limitation, but this is currently okay for me.

 

I now solved my problem by using two different queries. Each query just expands the needed data for one of the two dataTypes. Afterwards I just combine these queries and get the desired result. 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.