{"id":515,"date":"2024-12-07T16:13:31","date_gmt":"2024-12-07T16:13:31","guid":{"rendered":"https:\/\/datadandies.nl\/?p=515"},"modified":"2024-12-08T09:05:39","modified_gmt":"2024-12-08T09:05:39","slug":"creating-multiple-measures-for-a-power-bi-semantic-model-at-once-using-c-scripts-in-tabular-editor-2-0","status":"publish","type":"post","link":"https:\/\/datadandies.nl\/index.php\/2024\/12\/07\/creating-multiple-measures-for-a-power-bi-semantic-model-at-once-using-c-scripts-in-tabular-editor-2-0\/","title":{"rendered":"Creating multiple measures for a Power BI Semantic model at once using C# scripts in Tabular Editor 2.0"},"content":{"rendered":"\n<p>Imagine you have to create a whole lot of measures in Power BI that are similar.<\/p>\n\n\n\n<p>One free way to do this is, is by using C# scripts in Tabular Editor 2.0.<\/p>\n\n\n\n<p>Below is an example of a \u201cstatic\u201d way to do this. You simply make the necessary adjustments like changing the table name and a measure will be added according to the logic of the C# script below. However, what if you would like to make this script a little more dynamic?<\/p>\n\n\n\n<p>Static:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/Static: create a measure called &quot;Total TalkTime&quot; in Display Folder ..\n\/\/.. called &quot;MeasureFoldersStatic&quot; for the column column_name in table FactCalls\n\nvar c = (Model.Tables[&quot;FactCalls&quot;].Columns[&quot;TalkTime&quot;] as DataColumn);  \/\/Table and column \n\nc.Table.AddMeasure(                                 \/\/Adding a measure\n        &quot;Total &quot; + c.Name,                          \/\/Name of the measure\n        &quot;SUM(&quot;  + c.DaxObjectFullName + &quot;)&quot;,        \/\/DAX of the measure\n        c.DisplayFolder + &quot;MeasureFoldersStatic&quot;    \/\/Folder where the measure will be stored \n        );\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Kopieer\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\">\/\/Static: create a measure called &quot;Total TalkTime&quot; in Display Folder ..<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/.. called &quot;MeasureFoldersStatic&quot; for the column column_name in table FactCalls<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">var<\/span><span style=\"color: #D8DEE9FF\"> c <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">Model<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">Tables<\/span><span style=\"color: #ECEFF4\">[<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">FactCalls<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">].<\/span><span style=\"color: #D8DEE9\">Columns<\/span><span style=\"color: #ECEFF4\">[<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">TalkTime<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">]<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">as<\/span><span style=\"color: #D8DEE9FF\"> DataColumn<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\">\/\/Table and column <\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">c<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">Table<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">AddMeasure<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">                                 <\/span><span style=\"color: #616E88\">\/\/Adding a measure<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Total <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">c<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">Name<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\">                          <\/span><span style=\"color: #616E88\">\/\/Name of the measure<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">SUM(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">c<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">DaxObjectFullName<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">)<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #616E88\">\/\/DAX of the measure<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">c<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">DisplayFolder<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">MeasureFoldersStatic<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #616E88\">\/\/Folder where the measure will be stored <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"414\" src=\"https:\/\/datadandies.nl\/wp-content\/uploads\/2024\/12\/20241208-Adding-measures-to-PBI-using-C-static-1024x414.png\" alt=\"\" class=\"wp-image-516\" srcset=\"https:\/\/datadandies.nl\/wp-content\/uploads\/2024\/12\/20241208-Adding-measures-to-PBI-using-C-static-1024x414.png 1024w, https:\/\/datadandies.nl\/wp-content\/uploads\/2024\/12\/20241208-Adding-measures-to-PBI-using-C-static-300x121.png 300w, https:\/\/datadandies.nl\/wp-content\/uploads\/2024\/12\/20241208-Adding-measures-to-PBI-using-C-static-768x311.png 768w, https:\/\/datadandies.nl\/wp-content\/uploads\/2024\/12\/20241208-Adding-measures-to-PBI-using-C-static.png 1522w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The above example works well for a single measure, what if you need something more dynamic? Let\u2019s look at a script that can handle multiple columns.<\/p>\n\n\n\n<p>Then you could try the script below. This script will create a SUM of every column you have selected. Very interesting stuff.<\/p>\n\n\n\n<p>Based on which columns you select:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/Based on selected columns: create measures starting with &quot;Total &quot; in Display Folder ..\n\/\/.. called &quot;MeasureFolderSelected&quot; for the selected columns \n\nforeach(var c in Selected.Columns)                  \/\/Variable with selectec columns\n{\n    c.Table.AddMeasure(                             \/\/Adding a measure\n        &quot;Total &quot; + c.Name,                          \/\/Name of the measure\n        &quot;SUM(&quot;  + c.DaxObjectFullName + &quot;)&quot;,        \/\/DAX of the measure\n        c.DisplayFolder + &quot;MeasureFolderSelected&quot;   \/\/Folder where the measure will be stored\n        );\n}\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Kopieer\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\">\/\/Based on selected columns: create measures starting with &quot;Total &quot; in Display Folder ..<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/.. called &quot;MeasureFolderSelected&quot; for the selected columns <\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">foreach<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">var<\/span><span style=\"color: #D8DEE9FF\"> c <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Selected<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">Columns<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">                  <\/span><span style=\"color: #616E88\">\/\/Variable with selectec columns<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #D8DEE9\">c<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">Table<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">AddMeasure<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">                             <\/span><span style=\"color: #616E88\">\/\/Adding a measure<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Total <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">c<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">Name<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\">                          <\/span><span style=\"color: #616E88\">\/\/Name of the measure<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">SUM(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">c<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">DaxObjectFullName<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">)<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #616E88\">\/\/DAX of the measure<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">c<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">DisplayFolder<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">MeasureFolderSelected<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\">   <\/span><span style=\"color: #616E88\">\/\/Folder where the measure will be stored<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"475\" src=\"https:\/\/datadandies.nl\/wp-content\/uploads\/2024\/12\/20241208-Adding-measures-to-PBI-using-C-selected-columns-1024x475.png\" alt=\"\" class=\"wp-image-517\" srcset=\"https:\/\/datadandies.nl\/wp-content\/uploads\/2024\/12\/20241208-Adding-measures-to-PBI-using-C-selected-columns-1024x475.png 1024w, https:\/\/datadandies.nl\/wp-content\/uploads\/2024\/12\/20241208-Adding-measures-to-PBI-using-C-selected-columns-300x139.png 300w, https:\/\/datadandies.nl\/wp-content\/uploads\/2024\/12\/20241208-Adding-measures-to-PBI-using-C-selected-columns-768x356.png 768w, https:\/\/datadandies.nl\/wp-content\/uploads\/2024\/12\/20241208-Adding-measures-to-PBI-using-C-selected-columns.png 1522w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Inspiration source: <a href=\"https:\/\/www.youtube.com\/watch?v=EHs5r3XCkO8\">https:\/\/www.youtube.com\/watch?v=EHs5r3XCkO8<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Imagine you have to create a whole lot of measures in Power BI that are similar. One free way to do this is, is by using C# scripts in Tabular Editor 2.0. Below is an example of a \u201cstatic\u201d way to do this. You simply make the necessary adjustments like changing the table name and&hellip;<\/p>\n<p class=\"more-link\"><a href=\"https:\/\/datadandies.nl\/index.php\/2024\/12\/07\/creating-multiple-measures-for-a-power-bi-semantic-model-at-once-using-c-scripts-in-tabular-editor-2-0\/\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[51,3,50],"class_list":["post-515","post","type-post","status-publish","format-standard","hentry","category-blog","tag-c","tag-powerbi","tag-tabulareditor"],"_links":{"self":[{"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/posts\/515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/comments?post=515"}],"version-history":[{"count":2,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/posts\/515\/revisions"}],"predecessor-version":[{"id":519,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/posts\/515\/revisions\/519"}],"wp:attachment":[{"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/media?parent=515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/categories?post=515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/tags?post=515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}