1

This is my code in PS:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.AnalysisServices.Tabular")
$serverName = "panya\taylor";
$databaseName = "FreshDesk";
$tableName = "Metrics";
$measureName = "TestAuto";
$measureExpression = "SUMX ( FactTickets, FactTickets[PriorityID] * FactTickets[StatusID] )";

$svr = new-Object Microsoft.AnalysisServices.Tabular.Server
$svr.Connect($serverName)
$database = $svr.databases
$db = $database.GetByName($databaseName)
$table = $db.Model.Tables.Item($tableName)
$measure = new-Object Microsoft.AnalysisServices.Tabular.Measure
$measure.Name = $measureName
$measure.Expression = $measureExpression
$table.Measures.Add( $measure )
$db.Model.SaveChanges()

However it is giving me an error You cannot call a method on a null-valued expression. Can't figure it out. The error is thrown at this line:

$table = $db.Model.Tables.Item($tableName)

but none of the variables used here is NULL.

DNac
  • 2,663
  • 8
  • 31
  • 54
  • What does this `$database.GetByName($databaseName)` returns? Looks like it is null. – n01d Sep 14 '16 at 11:56
  • `write-host $database` returns list of databases, `write-host $db` returns the database I want to work with. – DNac Sep 14 '16 at 12:03
  • Does `$db` have a Model property? Does The model property have Tables... and so on. This error is correct and something is null. You cant use `(var).GetType().Fullname` to see type names and piping into Get-Member will help you here. I don't have the environment to test what the failure is here so you will have to do this to stop us guessing. – Matt Sep 14 '16 at 12:20
  • Comparing to [here](https://www.sqlbi.com/articles/adding-a-measure-to-a-tabular-model/) the code _looks_ correct. – Matt Sep 14 '16 at 12:23
  • @DNac `$db.Model.Tables` or `$db.Model` may be NULL as well. Try to use `get-member` on all of them. – n01d Sep 14 '16 at 12:56
  • @Matt sorry, didn't read your comment ^_^ – n01d Sep 14 '16 at 13:04
  • Sometimes found `Set-StrictMode -Version latest` helps debugging these sorts of things. – Burt_Harris Sep 15 '16 at 22:26

0 Answers0