获取 powerbi 文件内的数据(pbix 文档非打开方式)--获取查询语句

let
    fx = (path)=>
let
    Header = BinaryFormat.Record([
        MiscHeader = BinaryFormat.Binary(14),
        BinarySize = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32,0),
        FileSize   = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32,0),
        FileNameLen= BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16,0),
        ExtrasLen  = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16,0)]),

    HeaderChoice = BinaryFormat.Choice(
        BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32,0),
        each if _ <> 67324752 then BinaryFormat.Record([IsValid = false])
                              else BinaryFormat.Choice(BinaryFormat.Binary(26),      // Header payload - 14+4+4+2+2
                                       each BinaryFormat.Record([IsValid  = true,
                                                                 Filename = BinaryFormat.Text(Header(_)[FileNameLen]),
                                                                 Extras   = BinaryFormat.Text(Header(_)[ExtrasLen]),
                                                                 Content  = BinaryFormat.Transform(BinaryFormat.Binary(Header(_)[BinarySize]),
                                                                                (x) => try Binary.Buffer(Binary.Decompress(x, Compression.Deflate)) otherwise null)
                                                               ]),
                        type binary)),

    mb = BinaryFormat.List(HeaderChoice, each [IsValid])(File.Contents(path)){2}[Content],
    data=Xml.Tables("<LocalPackageMetadataFile"&
               Text.Split(List.Select(Lines.FromBinary(mb),each Text.Contains(_,"RootFormulaText")){0},"LocalPackageMetadataFile"){1}&
               "LocalPackageMetadataFile>"
               ){0}[Table]{0}[Table],
    list=Table.ExpandTableColumn(Table.ExpandTableColumn(data, "StableEntries",{"Entry"}),"Entry", {"Attribute:Value"})[#"Attribute:Value"],
    M=List.Select(list,each Text.Contains(_,"RootFormulaText"))
in
    List.Transform(M,each Json.Document(Text.Middle(_,1))[RootFormulaText])
in
fx        //使用时直接用fx(你的pbix文档绝对路径),如fx("C:\Users\Administrator\Desktop\pg.pbix")

file

file

道高一尺 魔高一丈
https://pbihub.cn/users/44
M与DAX的恩怨纠葛