我正在尝试使用 PowerShell 中的
Invoke-AsCmd
更改 Power BI 语义模型中现有度量的表达式。我还没有找到任何这方面的例子。到目前为止我所拥有的是这个 TSML alter 命令
$connectionString = "Data Source=powerbi://api.powerbi.com/v1.0/myorg/Workspace1"
$statement = [ordered]@{
alter = [ordered]@{
object= [ordered]@{
database = "test-database"
table = "Table1"
}
table = [ordered]@{
name = "Table1"
measures = @( [ordered]@{
name = "Measure1"
expression = "1 // new expression"
})
}
}
}
$query = $statement | ConvertTo-Json -Depth 5
Invoke-ASCmd -ConnectionString $connectionString -Query $query
运行时没有错误,结果是
但是服务器上的表达式并未更新。为服务器启用 XMLA 读/写功能。我尝试过研究 XMLA 并以这种方式编写更改命令,但我很难遵循文档。有人有以这种方式更新度量表达式的示例吗?
我设法使用 XMLA 的 Alter 语句找到答案
$query = @"
<Alter xmlns="http://schemas.microsoft.com/analysisservices/2014/engine">
<DatabaseID><![CDATA[$databaseId]]></DatabaseID>
<Measures>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<xs:element>
<xs:complexType>
<xs:sequence>
<xs:element type="row" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="row">
<xs:sequence>
<xs:element name="ID" type="xs:unsignedLong" sql:field="ID" minOccurs="0" />
<xs:element name="ID.Table" type="xs:string" sql:field="ID.Table" minOccurs="0" />
<xs:element name="ID.Measure" type="xs:string" sql:field="ID.Measure" minOccurs="0" />
<xs:element name="Name" type="xs:string" sql:field="Name" minOccurs="0" />
<xs:element name="Description" type="xs:string" sql:field="Description" minOccurs="0" />
<xs:element name="Expression" type="xs:string" sql:field="Expression" minOccurs="0" />
<xs:element name="FormatString" type="xs:string" sql:field="FormatString" minOccurs="0" />
<xs:element name="IsHidden" type="xs:boolean" sql:field="IsHidden" minOccurs="0" />
<xs:element name="IsSimpleMeasure" type="xs:boolean" sql:field="IsSimpleMeasure" minOccurs="0" />
<xs:element name="DisplayFolder" type="xs:string" sql:field="DisplayFolder" minOccurs="0" />
<xs:element name="DataCategory" type="xs:string" sql:field="DataCategory" minOccurs="0" />
<xs:element name="LineageTag" type="xs:string" sql:field="LineageTag" minOccurs="0" />
<xs:element name="SourceLineageTag" type="xs:string" sql:field="SourceLineageTag" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:schema>
<row xmlns="urn:schemas-microsoft-com:xml-analysis:rowset">
<ID><![CDATA[$measureId]></ID>
<Expression><![CDATA[$measureExpression]></Expression>
</row>
</Measures>
</Alter>
"@
Invoke-ASCmd -ConnectionString $connectionString -Query $query
我实际上是通过使用以下 .NET 代码捕获 XMLA 请求得到的
var server = new Microsoft.AnalysisServices.Tabular.Server();
server.Connect("<connection string>");
server.CaptureXml = true;
server.Databases["Dataset1"].Model.Tables["Table1"].Measures["Measure1"].Expression = "1 // new expression";
server.Databases["Dataset1"].Model.SaveChanges();
foreach (var item in server.CaptureLog)
{
File.AppendAllText("log.txt", item);
}