Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SqlWaldorf/e6f70d278e8350729f1485bccb75d7c1 to your computer and use it in GitHub Desktop.
Save SqlWaldorf/e6f70d278e8350729f1485bccb75d7c1 to your computer and use it in GitHub Desktop.
M script for the U2U course on Power BI
let
Source = Sql.Database("u2u-sql.database.windows.net", "UBIPB_AdventureWorksDW2014"),
dbo_FactInternetSales = Source{[Schema="dbo",Item="FactInternetSales"]}[Data],
#"Removed Columns" = Table.RemoveColumns(dbo_FactInternetSales,{"PromotionKey", "CurrencyKey", "SalesTerritoryKey", "RevisionNumber", "ExtendedAmount", "UnitPriceDiscountPct", "DiscountAmount", "ProductStandardCost", "CarrierTrackingNumber", "CustomerPONumber", "DueDate", "ShipDate", "DimCurrency", "DimCustomer", "DimDate(DueDateKey)", "DimDate(OrderDateKey)", "DimDate(ShipDateKey)", "DimProduct", "DimPromotion", "DimSalesTerritory", "FactInternetSalesReason"}),
#"Added Custom" = Table.AddColumn(#"Removed Columns", "Profit", each [SalesAmount]-[TotalProductCost]),
#"Changed Type" = Table.TransformColumnTypes(#"Added Custom",{{"Profit", type number}}),
#"Calculated Start of Quarter" = Table.TransformColumns(#"Changed Type",{{"OrderDate", Date.StartOfQuarter, type datetime}}),
#"Removed Columns1" = Table.RemoveColumns(#"Calculated Start of Quarter",{"OrderDateKey", "DueDateKey", "ShipDateKey", "CustomerKey", "SalesOrderNumber", "SalesOrderLineNumber", "OrderQuantity", "UnitPrice", "TotalProductCost", "SalesAmount", "TaxAmt", "Freight"}),
#"Grouped Rows" = Table.Group(#"Removed Columns1", {"ProductKey", "OrderDate"}, {{"Total Profit", each List.Sum([Profit]), type number}}),
#"Added Custom1" = Table.AddColumn(#"Grouped Rows", "Expected Profit", each Number.Round([Total Profit] * (([Total Profit] - Number.RoundDown([Total Profit]) - 0.5) * 0.3 + 1)/ 100) * 100),
#"Removed Columns2" = Table.RemoveColumns(#"Added Custom1",{"Total Profit"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns2",{{"OrderDate", type date}}),
#"Inserted Year" = Table.AddColumn(#"Changed Type1", "Year", each Date.Year([OrderDate]), Int64.Type),
#"Inserted Quarter" = Table.AddColumn(#"Inserted Year", "Quarter", each Date.QuarterOfYear([OrderDate]), Int64.Type),
#"Removed Columns3" = Table.RemoveColumns(#"Inserted Quarter",{"OrderDate"})
in
#"Removed Columns3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment