Power BI不区分大小写JSON

时间:2018-02-15 12:17:57

标签: json azure powerbi

我正在通过resources.azure.com上的API连接到Azure资源,从那里我正在使用Microsoft.Compute的API并通过JSON将所有VM详细信息导入Power BI。

导入工作正常,但是在某些情况下数据存在差异。例如,在处理tags值时,有些人键入了相同的单词但在不同的情况下,例如;

    "tags": {
      "Project": "DT",
      "SLStandard": "Yes"

相比
    "tags": {
      "project": "DT",
      "SlStandard": "Yes"

在Power BI中扩展列时,它会将上面列出的项目视为两个不同的值。

Power BI case sensitivity

理想情况下,我希望导入JSON并忽略“case”,或者将所有传入标记为大写或小写。

我已阅读下面的两个链接,但我是Power BI的新手,我不确定如何实现它,或者即使它是我需要的。

Case sensitivity in Power BIPower BI changing text case automaticallyhttp://www.thebiccountant.com/2016/10/27/tame-case-sensitivity-power-query-powerbi/

这是我的高级编辑器代码:

let
    iterations = 10,
    url = 
     "https://management.azure.com/subscriptions/< subscription id >/providers/Microsoft.Compute/virtualMachines?api-version=2017-12-01",

    FnGetOnePage =
     (url) as record =>
      let
       Source = Json.Document(Web.Contents(url)),
       data = try Source[value] otherwise null,
       next = try Source[nextLink] otherwise null,
       res = [Data=data, Next=next]
      in
       res,

    GeneratedList =
     List.Generate(
      ()=>[i=0, res = FnGetOnePage(url)],
      each [i]<iterations and [res][Data]<>null,
      each [i=[i]+1, res = FnGetOnePage([res][Next])],
      each [res][Data]),
    #"Converted to Table" = Table.FromList(GeneratedList, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandListColumn(#"Converted to Table", "Column1"),
    #"Expanded Column2" = Table.ExpandRecordColumn(#"Expanded Column1", "Column1", {"tags"}, {"Column1.tags"}),
    #"Expanded Column1.tags" = Table.ExpandRecordColumn(#"Expanded Column2", "Column1.tags", {"Project", "project", "SLStandard", "sLStandard", "BIOffline", "bIStandard", "AutomationBI", "biStandard", "BIStandard", "asdf-U001", "TestVM"}, {"Column1.tags.Project.1", "Column1.tags.project", "Column1.tags.SLStandard.1", "Column1.tags.sLStandard", "Column1.tags.BIOffline", "Column1.tags.bIStandard.1", "Column1.tags.AutomationBI", "Column1.tags.biStandard.2", "Column1.tags.BIStandard", "Column1.tags.asdf-U001", "Column1.tags.TestVM"})
in
    #"Expanded Column1.tags"

如果您想知道为什么我的查询导入的时间太长,请查看我之前的帖子:Power BI - Call Azure API with nextLink (next page)

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

今天我正在努力解决同样的问题。一种解决方法虽然不是很优雅,但要小写或大写上游的JSON。

作为临时解决方案为我工作。

希望它有所帮助, 克里斯

答案 1 :(得分:0)

您的问题似乎直接来自数据源以及数据源级别的字段名称之间的差异。

以下是关于如何强制/确保它们都具有相同外壳的示例代码:

let
  Source = {[A=24,b=53], [a=43,B=43], [a=3,b=3]},
  Custom1 = List.Transform(Source, (_)=> Record.RenameFields(_, List.Zip({Record.FieldNames(_), List.Transform(Record.FieldNames(_), Text.Lower)})))

答案 2 :(得分:0)

我已经在另一个论坛上对此进行了回答并尝试了它,它确实有效。

https://community.powerbi.com/t5/Desktop/Power-BI-case-insensitive-with-JSON/m-p/360134

“您可以在展开之前重命名您的记录字段(例如通过应用Text.Proper)(在最后一步中):”

Table.TransformColumns(#"Expanded Column2", {{"tags", each Record.RenameFields(_, List.Zip({Record.FieldNames(_), List.Transform(Record.FieldNames(_), (name)=> Text.Proper(name))}))}}),

最终输出如下:

let
    iterations = 10,
    url = 
     "https://management.azure.com/subscriptions/< subscription >/providers/Microsoft.Compute/virtualMachines?api-version=2017-12-01",

FnGetOnePage =
 (url) as record =>
  let
   Source = Json.Document(Web.Contents(url)),
   data = try Source[value] otherwise null,
   next = try Source[nextLink] otherwise null,
   res = [Data=data, Next=next]
  in
   res,

GeneratedList =
 List.Generate(
  ()=>[i=0, res = FnGetOnePage(url)],
  each [i]<iterations and [res][Data]<>null,
  each [i=[i]+1, res = FnGetOnePage([res][Next])],
  each [res][Data]),
#"Converted to Table" = Table.FromList(GeneratedList, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandListColumn(#"Converted to Table", "Column1"),
#"Expanded Column2" = Table.ExpandRecordColumn(#"Expanded Column1", "Column1", {"properties", "tags"}, {"properties", "tags"}),
#"Expanded properties" = Table.TransformColumns(#"Expanded Column2", {{"tags", each Record.RenameFields(_, List.Zip({Record.FieldNames(_), List.Transform(Record.FieldNames(_), (name)=> Text.Proper(name))}))}}),
#"Expanded properties1" = Table.ExpandRecordColumn(#"Expanded properties", "properties", {"vmId"}, {"properties.vmId"}),
#"Expanded tags" = Table.ExpandRecordColumn(#"Expanded properties1", "tags", {"Project", "Slstandard", "Bioffline", "Bistandard", "Automationbi", "asdf-U001", "Testvm"}, {"tags.Project", "tags.Slstandard", "tags.Bioffline", "tags.Bistandard", "tags.Automationbi", "tags.asdf-U001", "tags.Testvm"})
in
#"Expanded tags"

希望这有助于其他人!