Saturday, November 7, 2020

How to connect to CSV and Excel Files in SharePoint from Power BI

Power Query to Connect to CSV and Excel Files in SharePoint from Power BI
We can connect to the .CSV and .XLSX files using the Connection method as Web source, using the below Power Query methods :

1) Source : SharePoint Files :-

Comma Delimited CSV File (.csv) :
let
Source = Csv.Document(Web.Contents("https://SharePointServer/sites/RootFolder/Source/Input.csv"),[Delimiter="," , Encoding=65001, QuoteStyle=QuoteStyle.None]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"SalesOrder_Id", Int64.Type}, {"Order_Date", type date}, {"SalesRegion_Id", type text}, {"Supplier_Id", type text}, {"Cust_Id", type text}, {"Prod_Id", type text}, {"Units_Sold", Int64.Type}, {" Unit_Price ", Currency.Type}, {" Gross_Sales ", Currency.Type}, {" COGS ", Currency.Type}, {" Disc ", type text}, {" Net_Sales ", Currency.Type}})

in
#"Changed Type"


Excel File (.xlsx) :
let
Source = Excel.Workbook(Web.Contents("https://SharePointServer/sites/RootFolder/Source/Input.xlsx"),null, true),tbl_Sales_Sheet = Source{[Item="tbl_Sales",Kind="Sheet"]}[Data] ,
#"Promoted Headers" = Table.PromoteHeaders(tbl_Sales_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"SalesOrder_Id", Int64.Type}, {"Order_Date", type date}, {"SalesRegion_Id", type text}, {"Supplier_Id", type text}, {"Cust_Id", type text}, {"Prod_Id", type text}, {"Units_Sold", Int64.Type}, {"Unit_Price", Int64.Type}, {"Gross_Sales", Int64.Type}, {"COGS", type number}, {"Disc", type number}, {"Net_Sales", type number}})
in
#"Changed Type"


Notes:
here, Item="SheetName"


2) Source : Local Files :-

Tab Delimited Text File(.txt) :
let
Source = Csv.Document(File.Contents(""T:\T_Tech_Lab\PowerBI_Lab\DataSets\txt_TabDelimited_Input.txt"),[Delimiter="#(tab)", Columns=12, Encoding=1252]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"SalesOrder_Id", Int64.Type}, {"Order_Date", type date}, {"SalesRegion_Id", type text}, {"Supplier_Id", type text}, {"Cust_Id", type text}, {"Prod_Id", type text}, {"Units_Sold", Int64.Type}, {" Unit_Price ", Currency.Type}, {" Gross_Sales ", Currency.Type}, {" COGS ", Currency.Type}, {" Disc ", type text}, {" Net_Sales ", Currency.Type}})
in
#"Changed Type"


Comma Delimited CSV File (.csv) :
let
Source = Csv.Document(File.Contents(""T:\T_Tech_Lab\PowerBI_Lab\DataSets\csv_Input.csv"),[Delimiter=",", Columns=12, Encoding=65001, QuoteStyle=QuoteStyle.None]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"SalesOrder_Id", Int64.Type}, {"Order_Date", type date}, {"SalesRegion_Id", type text}, {"Supplier_Id", type text}, {"Cust_Id", type text}, {"Prod_Id", type text}, {"Units_Sold", Int64.Type}, {" Unit_Price ", Currency.Type}, {" Gross_Sales ", Currency.Type}, {" COGS ", Currency.Type}, {" Disc ", type text}, {" Net_Sales ", Currency.Type}})
in
#"Changed Type"


Excel File (.xlsx) :
let
Source = Excel.Workbook(File.Contents("T:\T_Tech_Lab\PowerBI_Lab\DataSets\ds_Sample.xlsx"), null, true),
tbl_Sales_Sheet = Source{[Item="tbl_Sales",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(tbl_Sales_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"SalesOrder_Id", Int64.Type}, {"Order_Date", type date}, {"SalesRegion_Id", type text}, {"Supplier_Id", type text}, {"Cust_Id", type text}, {"Prod_Id", type text}, {"Units_Sold", Int64.Type}, {"Unit_Price", Int64.Type}, {"Gross_Sales", Int64.Type}, {"COGS", type number}, {"Disc", type number}, {"Net_Sales", type number}})
in
#"Changed Type"

Thanks, Tamatam

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.