Skip to content

Instantly share code, notes, and snippets.

@ImkeF
Last active May 21, 2020 01:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ImkeF/7f7431ea70ff6f702b5317edf738dae0 to your computer and use it in GitHub Desktop.
Save ImkeF/7f7431ea70ff6f702b5317edf738dae0 to your computer and use it in GitHub Desktop.
Returns port number and database name of the latest Power BI data model opened with PBI Desktop.
let func =
(optional Model as text) =>
let
Source_Port = Folder.Files("C:\Users"),
msmdsrv = Table.SelectRows(Source_Port, each [Name] = "msmdsrv.port.txt"),
#"Sorted Rows" = Table.Buffer(Table.Sort(msmdsrv,{{"Date created", Order.Descending}})),
Select_Last_Instance_Active = Table.FirstN(#"Sorted Rows",1),
#"Combined Binaries" = Binary.Combine(Select_Last_Instance_Active[Content]),
#"Imported Text" = Table.FromColumns({Lines.FromBinary(#"Combined Binaries",null,null,1252)}),
PortID = () => Table.TransformColumns(#"Imported Text",{},Text.Clean){0}[Column1],
Database = () => AnalysisServices.Database("localhost: "&PortID(), "", [Query="select * from $SYSTEM.DISCOVER_PROPERTIES"]){0}[Value],
ConnectionDetails = #table({"Server", "Database"}, {{"localhost:" & PortID(), Database()}}),
Import_SSAS = if Model = null then ConnectionDetails else AnalysisServices.Database("localhost: "&PortID(), Database()){[Id="Model"]}[Data][Data]{0}
in
Import_SSAS ,
documentation = [
Documentation.Name = " PowerBI.DataModel ",
Documentation.Description = " Returns port number and database name of the latest Power BI data model opened with PBI Desktop. ",
Documentation.LongDescription = " Returns port number and database name of the latest Power BI data model opened with PBI Desktop. Optional parameter connects directly to the model itself. ",
Documentation.Category = " AccessingData ",
Documentation.Source = " www.TheBIccountant.com . https://www.thebiccountant.com/2016/04/09/hackpowerbi/ ",
Documentation.Version = " 1.0 ",
Documentation.Author = " Imke Feldmann: www.TheBIccountant.com. https://www.thebiccountant.com/2016/04/09/hackpowerbi/ ",
Documentation.Examples = {[Description = " ",
Code = " ",
Result = " "]}]
in
Value.ReplaceType(func, Value.ReplaceMetadata(Value.Type(func), documentation))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment