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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
cgkas
Helper V
Helper V

Tabulate text file formatted in blocks

Hi to all,

 

Maybe someone could help me withi this. Thanks in advance.

 

I have a file formatted like below 

 

 

 

 

some text
some text

rule "Kod hu167"	
when	
    g: CommandInput(input["OPERATION"] == 	9987)
then	
    response.setEntry("XYZ", "mC56")	
    response.setEntry("AA GHY", "No")	
    response.setEntry("SRV", "No A")	
end	

some text
	
rule "Kod 12"	
when	
    g: CommandInput(input["UPDATE"] == 	12)
then	
    response.setEntry("XYZ", "PE23r")	
    response.setEntry("FF3", "No")	
    response.setEntry("MAU", "No M")	
    response.setEntry("AA GHY", "No V")	
    response.setEntry("SRV", "No B")	
end	
	
rule "Kod 34"	
when	
    g: CommandInput(input["DELETE"] == 	34)
then	
    response.setEntry("XYZ", "tTY87")	
end	

 

 

 

 

Which I'd like to tabulate as shown in this image

 

image.png

 

Values for column RULE are getting from lines rule "XXX..." 

Values for column INPUT TYPE are getting from lines g: CommandInput(...)

Values VAL for column INPUT ID are getting from lines g: CommandInput(... == VAL)

 

Then, between the block "then - end" come several lines contaningresponse.setEntry("COLUMN Nth", "VAL") and each different first value (COLUMN Nth) insideresponse.setEntry("COLUMN Nth", "VAL") would be the name of a new column and values will be the second string (VAL)

 

This is the input text in binary:

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZDBCoJAEIZfZZiTQQRlWQQettpKKpOyyFwPgUsFuYquRG+fGUVdMp3DzB7+Yef7XBfj9MKB4Sz04ZQ2tS5D9OouXk9c5A/I6tiHYRgEB+EbIkqlcn50l+HSoitiG0uToQe6DvmC/NyMeRKFIuGNhEsqZHxTGO6cPcN69mcw7GgMaz/DhMBk6jzzZliUXq+2ryiQV5gLP595+8BttsqwbqwRsWk1UIu21Ljo9vFY/RdzQTZvzEU5g7At43BQ5FBtl3E4onNa1aG0nV736xzvDg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t])
in
    Source

 

 

 

1 ACCEPTED SOLUTION
wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1677729408814.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZDBCoJAEIZfZZiTQQRlWQQettpKKpOyyFwPgUsFuYquRG+fGUVdMp3DzB7+Yef7XBfj9MKB4Sz04ZQ2tS5D9OouXk9c5A/I6tiHYRgEB+EbIkqlcn50l+HSoitiG0uToQe6DvmC/NyMeRKFIuGNhEsqZHxTGO6cPcN69mcw7GgMaz/DhMBk6jzzZliUXq+2ryiQV5gLP595+8BttsqwbqwRsWk1UIu21Ljo9vFY/RdzQTZvzEU5g7At43BQ5FBtl3E4onNa1aG0nV736xzvDg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    Custom1 = let
                 fx=(t,s)=>Text.StartsWith(Text.Trim(t),s),
                 TBD=(x,y,z)=>Text.BetweenDelimiters(x,y,z),
                 Txt2Table=(t)=>Table.Combine(
                                              Table.Group(
                                                          t,
                                                          "Column1",
                                                          {"n",each let
                                                                       r=List.Accumulate(
                                                                                         [Column1],
                                                                                         [],
                                                                                         (x,y)=>if fx(y,"rule ") then x&[RULE=TBD(y,"""","""")] else
                                                                                                if fx(y,"g: ") then x&[INPUT TYPE=TBD(y,"""",""""),INPUT ID=Text.Trim(TBD(y,"==",")"))] else
                                                                                                if fx(y,"response.") then x&Record.AddField([],TBD(y,"(""",""","),TBD(y,", """,""")")) else x
                                                                                        )
                                                                    in
                                                                      if r=[] then #table(0,{})
                                                                      else Table.FromRecords({r})
                                                          },
                                                          0,
                                                          (x,y)=>Byte.From(fx(y,"rule "))
                                                         )[n]
                                             )
               in
                 Txt2Table(Source)
in
    Custom1

 

View solution in original post

4 REPLIES 4
cgkas
Helper V
Helper V

@wdx223_Daniel Thanks so much. I'm surprised. All conversion in one step. 

 

I don't want to bother you by asking for an explanation of the logic in your solution. Maybe you could give me some reference where I can learn how to do this type of custom functions, without having to do several steps. For example, how do you test this multi level nesting? How do you debug if everything is in one step?

 

wdx223_Daniel_0-1677736050816.png

here is some explanations.

 

Thank you again

 

wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1677729408814.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZDBCoJAEIZfZZiTQQRlWQQettpKKpOyyFwPgUsFuYquRG+fGUVdMp3DzB7+Yef7XBfj9MKB4Sz04ZQ2tS5D9OouXk9c5A/I6tiHYRgEB+EbIkqlcn50l+HSoitiG0uToQe6DvmC/NyMeRKFIuGNhEsqZHxTGO6cPcN69mcw7GgMaz/DhMBk6jzzZliUXq+2ryiQV5gLP595+8BttsqwbqwRsWk1UIu21Ljo9vFY/RdzQTZvzEU5g7At43BQ5FBtl3E4onNa1aG0nV736xzvDg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    Custom1 = let
                 fx=(t,s)=>Text.StartsWith(Text.Trim(t),s),
                 TBD=(x,y,z)=>Text.BetweenDelimiters(x,y,z),
                 Txt2Table=(t)=>Table.Combine(
                                              Table.Group(
                                                          t,
                                                          "Column1",
                                                          {"n",each let
                                                                       r=List.Accumulate(
                                                                                         [Column1],
                                                                                         [],
                                                                                         (x,y)=>if fx(y,"rule ") then x&[RULE=TBD(y,"""","""")] else
                                                                                                if fx(y,"g: ") then x&[INPUT TYPE=TBD(y,"""",""""),INPUT ID=Text.Trim(TBD(y,"==",")"))] else
                                                                                                if fx(y,"response.") then x&Record.AddField([],TBD(y,"(""",""","),TBD(y,", """,""")")) else x
                                                                                        )
                                                                    in
                                                                      if r=[] then #table(0,{})
                                                                      else Table.FromRecords({r})
                                                          },
                                                          0,
                                                          (x,y)=>Byte.From(fx(y,"rule "))
                                                         )[n]
                                             )
               in
                 Txt2Table(Source)
in
    Custom1

 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.