我用Slack搜集PowerBI的資訊,今天突然發現Slack可以導出數據,于是全部導出。
不足19M的導出文件,下載了好久。不知是否沒翻墻的緣故。
導出的文件是按channel來分的,每個channel下都包含一個“年-月-日.json”的文件,所以,凡是某天獲取到了消息,就會有一個json文件,該天的所有消息都包含在這個json文件中。
既然有幾百個json文件需要處理,我第一反應是建立一個從文件夾獲取的源,就像處理csv那樣,期待power bi desktop能夠自動創建函數,自動合并這些json文件。可惜這次power bi desktop出錯了。
所以此路不通。
最近剛好對自定義函數感興趣,那么嘗試用自定義函數來解決此問題。
首先按照創建自定義函數的步驟,引入一個單獨的json文件,對其進行處理。
let
源 = Json.Document(File.Contents(para)),
轉換為表 = Table.FromList(源, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"展開的“Column1”" = Table.ExpandRecordColumn(轉換為表, "Column1", {"text", "bot_id", "attachments", "type", "subtype", "ts"}, {"text", "bot_id", "attachments", "type", "subtype", "ts"}),
刪除的其他列 = Table.SelectColumns(#"展開的“Column1”",{"attachments"}),
#"展開的“attachments”" = Table.ExpandListColumn(刪除的其他列, "attachments"),
#"展開的“attachments”1" = Table.ExpandRecordColumn(#"展開的“attachments”", "attachments", {"author_name", "fallback", "text", "id", "author_link", "author_icon", "color", "fields", "mrkdwn_in"}, {"author_name", "fallback", "text", "id", "author_link", "author_icon", "color", "fields", "mrkdwn_in"}),
刪除的副本 = Table.Distinct(#"展開的“attachments”1", {"text"})
in
刪除的副本
注意第一句中的“jsonpath”是實際的單獨json文件所在的全路徑,要加引號。
處理完畢后,把“源”這一步中的FileContents括號中的內容全部換成一個參數,比如就用jsonpath。然后在PowerBI desktop中新建一個同名參數,把剛才的文件路徑作為其值。
接下來,在PowerBI desktop中窗口左側,選中剛才這個查詢,右鍵選擇,創建為函數。下面為創建好的函數代碼。注意我用的參數是para
let
源 = (para as text) => let
源 = Json.Document(File.Contents(para)),
轉換為表 = Table.FromList(源, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"展開的“Column1”" = Table.ExpandRecordColumn(轉換為表, "Column1", {"text", "bot_id", "attachments", "type", "subtype", "ts"}, {"text", "bot_id", "attachments", "type", "subtype", "ts"}),
刪除的其他列 = Table.SelectColumns(#"展開的“Column1”",{"attachments"}),
#"展開的“attachments”" = Table.ExpandListColumn(刪除的其他列, "attachments"),
#"展開的“attachments”1" = Table.ExpandRecordColumn(#"展開的“attachments”", "attachments", {"author_name", "fallback", "text", "id", "author_link", "author_icon", "color", "fields", "mrkdwn_in"}, {"author_name", "fallback", "text", "id", "author_link", "author_icon", "color", "fields", "mrkdwn_in"}),
刪除的副本 = Table.Distinct(#"展開的“attachments”1", {"text"})
in
刪除的副本
in
源
函數創建好之后,可以對其命名以免自己弄混掉,也可以保持原名稱。我將其命名為“json”。
接下來的步驟是我們要將json這個自定義函數應用到文件夾下每一個文件上。而應用的辦法就是構造這個自定義函數的參數值,這個參數值是一個全路徑。因此,按照處理文件夾下所有文件的步驟,建立一個引入文件夾的源,將該文件夾下所有的文件引入進來。
let
源 = Folder.Files("C:\Users\wanght\Desktop\general"),
刪除的其他列 = Table.SelectColumns(源,{"Name", "Folder Path"}),
已添加自定義 = Table.AddColumn(刪除的其他列, "自定義", each json([Folder Path]&[Name])),
#"展開的“自定義”" = Table.ExpandTableColumn(已添加自定義, "自定義", {"user", "type", "subtype", "text", "ts"}, {"user", "type", "subtype", "text", "ts"}),
篩選的行 = Table.SelectRows(#"展開的“自定義”", each ([text] <> "")),
刪除的副本 = Table.Distinct(篩選的行, {"text"}),
刪除的其他列1 = Table.SelectColumns(刪除的副本,{"Name", "text"}),
替換的值 = Table.ReplaceValue(刪除的其他列1,".json","",Replacer.ReplaceText,{"Name"}),
更改的類型 = Table.TransformColumnTypes(替換的值,{{"Name", type date}}),
重命名的列 = Table.RenameColumns(更改的類型,{{"Name", "日期"}})
in
重命名的列
第一步源是引入文件夾作為源。
第二步我選擇了僅保留“Name”和“Folder Path”兩列。保留“Name”的原因是因為其中包含了文件名,而“Folder Path”則包含了路徑。在下一步我們將利用這兩列每一行的值構造前面自定義函數json的參數(那是一個全路徑)。
第三步是關鍵操作,新增一列,引入自定義函數json,并用上一步保留的兩列來構造函數json的參數。
之后的步驟是對數據本身進行處理了,超出本文范圍,不再贅述。