<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by PBI DataVizzle on Medium]]></title>
        <description><![CDATA[Stories by PBI DataVizzle on Medium]]></description>
        <link>https://medium.com/@pbi-datavizzle?source=rss-23e0d5d396ad------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Beic17YTx8NzoWxo1z9o_A.png</url>
            <title>Stories by PBI DataVizzle on Medium</title>
            <link>https://medium.com/@pbi-datavizzle?source=rss-23e0d5d396ad------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 12:51:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pbi-datavizzle/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Deneb & Vega-Lite and PBI Core Visuals | SVG Images (Part 2)]]></title>
            <link>https://pbi-datavizzle.medium.com/deneb-vega-lite-and-pbi-core-visuals-svg-images-part-2-6bb623b98de2?source=rss-23e0d5d396ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/6bb623b98de2</guid>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[vega-lite]]></category>
            <category><![CDATA[power-bi]]></category>
            <category><![CDATA[deneb]]></category>
            <dc:creator><![CDATA[PBI DataVizzle]]></dc:creator>
            <pubDate>Sat, 01 Feb 2025 17:03:38 GMT</pubDate>
            <atom:updated>2025-02-01T17:03:38.361Z</atom:updated>
            <content:encoded><![CDATA[<h4>Several methods on how to implement SVG Images and SVG Paths in both Core Visuals and Deneb (Vega-Lite)🕊️ 🧙🏼‍♂️ ✨</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eUvKzwolk68J1xzg55OjMg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZbgDarqyTp_GPr93Ot9kkA.png" /></figure><p>💌 <em>PBIX file available at the end of the article… Enjoy!</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AgOdwohrtLArrvumkl1Q5Q.png" /></figure><h3>Recap:</h3><p>In <a href="https://medium.com/@pbiqueryous/deneb-vega-lite-and-pbi-core-visuals-svg-images-part-1-9e3d8cd77351">Part 1</a>, we looked at SVG Images in PBI Core Visuals, namely, in a table visual. In Part 2, we will apply a variation of these techniques to achieve similar results in Deneb.</p><p><strong><em>Important Note: </em></strong><em>There is a lot of good quality literature on this topic by our friends in the PBI Community, much more detailed and comprehensive than I provide here🤓I’ve included a list of resources and links at the end of article🪄🫶🏽</em></p><h3>Part 2: Getting SVG Images into Deneb (Vega-Lite)</h3><p>We dive right into it, head first, picking up from where we left off in <a href="https://medium.com/@pbiqueryous/deneb-vega-lite-and-pbi-core-visuals-svg-images-part-1-9e3d8cd77351">Part 1</a> where we have already done most of the heavy lifting 🤓</p><p>Let’s begin by pointing out the two main differences between what I will refer to as the <strong>SVG Code </strong>required for PBI Core Visuals, and the <strong>SVG Paths</strong> required for Deneb.</p><h4>SVG Code</h4><p>When I refer to <strong>SVG Code</strong> I intend to refer to the entire XML-based structure used to define an SVG image. It can include multiple elements (e.g., &lt;circle&gt;, &lt;rect&gt;, &lt;text&gt;, &lt;path&gt;) along with their attributes (like fill, stroke, etc.). In other words, “SVG Code” encompasses all the markup that describes your graphic—like a miniature “HTML document” for vector shapes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r0cFATpHvlkMCf2h0houIg.png" /><figcaption>SVG Code with Paths highlighted</figcaption></figure><h4>SVG Path</h4><p><strong>SVG Paths</strong> (&lt;path&gt; elements) are <em>one specific type</em> of SVG element. They define shapes through a set of drawing commands (e.g., M, L, C, Q, etc.) that tell the browser how to “move” a virtual pen and draw lines or curves. Paths are extremely flexible and can represent quite complex shapes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6VYXno3qtazb6c0rAvIYHg.png" /><figcaption>SVG Paths (separated for emphasis)</figcaption></figure><h4>SVG Image</h4><p>An SVG Image is the SVG output, comprising of one or more paths and/or other elements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/470/1*vUGgJgqE92HwKd5_QQD9PQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MzLINQ42XTX_WwrRcP1ixA.png" /></figure><h4>Utilising SVG Paths in Deneb (Vega-Lite)</h4><p>Time to roll your sleeves up and make some magic. Bring it on…✊🏼</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*onAItX2T6_K90bSBjWwSXg.gif" /></figure><p>There are a few processes we need to follow before we bring our SVG images to life in Deneb:</p><ol><li>Find an image ✅</li><li>Generate SVG code ✅</li><li>Extract SVG paths 🔎</li><li>Encode SVG paths within Deneb</li></ol><p>We’ve already looked at steps 1. and 2. above. Let’s look at two main ways to do extract SVG paths:</p><h4>3. Extract SVG Paths</h4><p><strong>3a) The Manual Way</strong></p><p>Basically, this is the surest way to extract your SVG paths, but also.. probably the most painful. Simply open up your SVG file, and manually copy/cut &amp; paste your SVG paths. So you go from this:</p><pre>&lt;svg width=&quot;122&quot; height=&quot;122&quot; viewBox=&quot;0 0 122 122&quot; fill=&quot;none&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;<br>&lt;g clip-path=&quot;url(#clip0_269_100)&quot;&gt;<br>&lt;path d=&quot;M61 122C94.6894 122 122 94.6894 122 61C122 27.3106 94.6894 0 61 0C27.3106 0 0 27.3106 0 61C0 94.6894 27.3106 122 61 122Z&quot; fill=&quot;#FFD02C&quot;/&gt;<br>&lt;path d=&quot;M61.05 92.42C61.05 92.42 69.8501 92.71 76.2701 86.57C82.69 80.43 79.41 74.15 77.98 72.3C76.56 70.45 61.0399 54.9 61.0399 54.9C61.0399 54.9 45.53 70.45 44.11 72.3C42.68 74.16 39.4 80.43 45.82 86.57C52.24 92.7 61.0399 92.42 61.0399 92.42M61.0399 37.73L73.9001 24.61L90.98 24.78L69.2801 46.36L85.63 63.35C85.63 63.35 99.4 75.74 86.6 88.46C77.55 97.44 61.05 97.38 61.05 97.38C61.05 97.38 44.55 97.45 35.5 88.46C22.69 75.74 36.47 63.35 36.47 63.35L52.81 46.36L31.11 24.78L48.18 24.61L61.0399 37.73Z&quot; fill=&quot;black&quot;/&gt;<br>&lt;/g&gt;<br>&lt;defs&gt;<br>&lt;clipPath id=&quot;clip0_269_100&quot;&gt;<br>&lt;rect width=&quot;122&quot; height=&quot;122&quot; fill=&quot;white&quot;/&gt;<br>&lt;/clipPath&gt;<br>&lt;/defs&gt;<br>&lt;/svg&gt;</pre><p>…to this:</p><pre>M61 122C94.6894 122 122 94.6894 122 61C122 27.3106 94.6894 0 61 0C27.3106 0 0 27.3106 0 61C0 94.6894 27.3106 122 61 122Z<br>M61.05 92.42C61.05 92.42 69.8501 92.71 76.2701 86.57C82.69 80.43 79.41 74.15 77.98 72.3C76.56 70.45 61.0399 54.9 61.0399 54.9C61.0399 54.9 45.53 70.45 44.11 72.3C42.68 74.16 39.4 80.43 45.82 86.57C52.24 92.7 61.0399 92.42 61.0399 92.42M61.0399 37.73L73.9001 24.61L90.98 24.78L69.2801 46.36L85.63 63.35C85.63 63.35 99.4 75.74 86.6 88.46C77.55 97.44 61.05 97.38 61.05 97.38C61.05 97.38 44.55 97.45 35.5 88.46C22.69 75.74 36.47 63.35 36.47 63.35L52.81 46.36L31.11 24.78L48.18 24.61L61.0399 37.73Z<br></pre><p><strong>3b) The Semi-automated way (with Power Query)</strong></p><p>There is an alternative way you could use to extract your paths in bulk. To achieve this I created a custom function in PowerQuery. I made some broad assumptions to get it to work, so I can’t confirm it is pure perfection, but it most certainly works in my case</p><p>i. Take you full SVG code, and input into Excel and format as a table.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7-av6UOU47PC16hfuMnWCA.png" /></figure><p>ii. Get the table into PowerQuery — use Get Data → from Table/Range:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*dtzZSUEdbBtkPKqY9UuSKA.png" /></figure><p>iii. Once in PowerQuery, add the following text in a new blank query, this will be our custom function:</p><pre>let<br>    // function<br>    fn_get_svg_paths = (svg_url_column as text) =&gt;<br>    let<br><br>        // create a record with multiple steps<br>        svg_records = <br>        [ <br>            // split svg into indiviual lines<br>            _split_paths = Text.Split( svg_url_column , &quot;#(lf)&quot;),<br><br>            // select lines with svg paths<br>            _select_paths = List.Select( _split_paths, each Text.Contains(_, &quot;path d=&quot;) or Text.Contains(_, &quot;path&quot;) or Text.Contains(_, &quot;d=\&quot;)),<br><br>            // extract svg paths between svg block delimiters<br>            _extract_paths = List.RemoveNulls(List.Transform( _select_paths, each try Text.Trim(Text.BetweenDelimiters(_, &quot;d=&#39;&quot;, &quot;&#39;&quot;)) otherwise null)),<br><br>            // combine svg paths into single line text<br>            _combine_paths = Text.Combine(_extract_paths, &quot;&quot;)<br><br>        // return single column &quot;_combine_paths&quot;<br>        ][_combine_paths]<br><br>    in<br>        svg_records<br><br>in<br>    // invoke function<br>    fn_get_svg_paths</pre><p>iv. Added custom function as column. Paste and go!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ePofJcrcFY8H4vPHYKwVrQ.png" /></figure><h4>4. Adjust SVG Paths</h4><p>When you first apply you SVG paths to your PowerBI report, you will very likely encounter some strange behaviours with SVG size and positioning. Fortunately, we take a few preparatory steps to guarantee pixel-perfect SVG joy 🤓 — this MIGHT get a little wild at first, so hold on tight</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*KG9VGD_kNST8Pw4CCBWS2w.gif" /></figure><p>To better observe what our SVG will look like *before* we add it to Deneb, we can use a wonderful tool by <a href="https://github.com/Yqnn/svg-path-editor?tab=readme-ov-file#svgpatheditor">Yann Armelin</a> called <a href="https://yqnn.github.io/svg-path-editor/">SVG Path Editor</a>.</p><p>Looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wu241IFZHu1U0C6mVVvdOQ.png" /><figcaption>SVG Path Editor Interface</figcaption></figure><p>We can test this example <a href="https://yqnn.github.io/svg-path-editor/#P=M61_122C94.6894_122_122_94.6894_122_61_122_27.3106_94.6894_0_61_0_27.3106_0_0_27.3106_0_61_0_94.6894_27.3106_122_61_122ZM61.05_92.42C61.05_92.42_69.8501_92.71_76.2701_86.57_82.69_80.43_79.41_74.15_77.98_72.3_76.56_70.45_61.0399_54.9_61.0399_54.9_61.0399_54.9_45.53_70.45_44.11_72.3_42.68_74.16_39.4_80.43_45.82_86.57_52.24_92.7_61.0399_92.42_61.0399_92.42M61.0399_37.73_73.9001_24.61_90.98_24.78_69.2801_46.36_85.63_63.35C85.63_63.35_99.4_75.74_86.6_88.46_77.55_97.44_61.05_97.38_61.05_97.38_61.05_97.38_44.55_97.45_35.5_88.46_22.69_75.74_36.47_63.35_36.47_63.35L52.81_46.36_31.11_24.78_48.18_24.61_61.0399_37.73Z">below</a>:</p><pre>M61 122C94.6894 122 122 94.6894 122 61 122 27.3106 94.6894 0 61 0 27.3106 0 0 27.3106 0 61 0 94.6894 27.3106 122 61 122ZM61.05 92.42C61.05 92.42 69.8501 92.71 76.2701 86.57 82.69 80.43 79.41 74.15 77.98 72.3 76.56 70.45 61.0399 54.9 61.0399 54.9 61.0399 54.9 45.53 70.45 44.11 72.3 42.68 74.16 39.4 80.43 45.82 86.57 52.24 92.7 61.0399 92.42 61.0399 92.42M61.0399 37.73 73.9001 24.61 90.98 24.78 69.2801 46.36 85.63 63.35C85.63 63.35 99.4 75.74 86.6 88.46 77.55 97.44 61.05 97.38 61.05 97.38 61.05 97.38 44.55 97.45 35.5 88.46 22.69 75.74 36.47 63.35 36.47 63.35L52.81 46.36 31.11 24.78 48.18 24.61 61.0399 37.73Z</pre><p>Paste it in your path box here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/1*Vyb28o7FtSFZ84A0GXfJjA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1015/1*kYg_ah4qBCcv7hA5-Z5Lgw.png" /></figure><p>The first thing to notice is the commands on the bottom right, these are the SVG path coordinates, and the SVG image on the right. We can determine the position of the SVG based on its position on the grid (red arrow below):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1010/1*YkkNrlQIK-72JNnz5Dy3wQ.png" /></figure><p>To centre the SVG appropriately, follow these steps:</p><ol><li>Hit the button “Convert to Relative”. This makes each SVG line/coordinate relative to each other. This makes it much each to reposition our SVG</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ca5MYBSxuBz_NR7eIeSvcQ.png" /></figure><p>2. Next to centre the SVG, we will adjust the first set of coordinates prefixed ‘m’. It is currently 61 122, let’s change this to 0 61</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ibN9PzFG9v11puvn2jvSKA.png" /></figure><p>Perfection! 🤓</p><p>3. Next we need to adjust the size of the SVG. This tool helps us understand again, that each square in the grid represents 1 pixel. If we zoom in a little, we can see more clearly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/451/1*SvGLJbz3FaQQ8peoce79gg.png" /></figure><p>This is a breeze, head over to Path Operations section of the panel, and adjust the scale. We want to reduce it quite a bit, so I’ll adjust the Scale X and Scale Y to 0.25 each.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4tDaRLO7ZWR3exhj3qmAwQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1fZyRt4d-Bjy6dcAQyYfGg.png" /><figcaption>Before and after</figcaption></figure><p>Keep reducing until its a few pixels on either side — give or take a few pixels</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*beL2DBMkUMwYAEmO5gemVg.png" /></figure><p>Our path is now ready to serve!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*2wR2t_0npvTjxG65-6ADYw.gif" /><figcaption>SVG recipe is cooking…</figcaption></figure><p>— -</p><ol><li>Find an image ✅</li><li>Generate SVG code ✅</li><li>Extract SVG paths ✅</li><li>Encode SVG paths within Deneb 🔎</li></ol><h4>5. Add SVG paths to Deneb</h4><p>Now for the fun part… I shall keep it brief, but once you have once SVG in place, you can then quickly add more by copy and pasting new SVG marks.</p><p>There are two methods, either are suitable depending on your preference.</p><p>5a. Params Method</p><pre>// in config section<br>&quot;params&quot;: [{<br>      &quot;name&quot;: &quot;pSNP&quot;,<br>      &quot;value&quot;: &quot;m0 3.0253c1.6598 0 3.0054-1.3456 3.0054-3.0054 0-1.6598-1.3456-3.0054-3.0054-3.0054-1.6598 0-3.0054 1.3456-3.0054 3.0054 0 1.6598 1.3456 3.0054 3.0054 3.0054zm.0025-1.4574c0 0 .4336.0143.7499-.2882.3163-.3025.1547-.6119.0842-.7031-.07-.0911-.8346-.8573-.8346-.8573 0 0-.7642.7661-.8341.8573-.0705.0916-.2321.4006.0842.7031.3163.302.7499.2882.7499.2882m0-2.6945.6336-.6464.8415.0084-1.0691 1.0632.8055.8371c0 0 .6784.6104.0478 1.2371-.4459.4424-1.2588.4395-1.2588.4395 0 0-.8129.0034-1.2588-.4395-.6311-.6267.0478-1.2371.0478-1.2371l.805-.8371-1.0691-1.0632.841-.0084.6336.6464z&quot;<br>    }]</pre><p>5b. Styles Method</p><pre>// in config section<br>&quot;style&quot;: {<br>      &quot;snp&quot;: {<br>      &quot;shape&quot;: &quot;m0 3.0253c1.6598 0 3.0054-1.3456 3.0054-3.0054 0-1.6598-1.3456-3.0054-3.0054-3.0054-1.6598 0-3.0054 1.3456-3.0054 3.0054 0 1.6598 1.3456 3.0054 3.0054 3.0054zm.0025-1.4574c0 0 .4336.0143.7499-.2882.3163-.3025.1547-.6119.0842-.7031-.07-.0911-.8346-.8573-.8346-.8573 0 0-.7642.7661-.8341.8573-.0705.0916-.2321.4006.0842.7031.3163.302.7499.2882.7499.2882m0-2.6945.6336-.6464.8415.0084-1.0691 1.0632.8055.8371c0 0 .6784.6104.0478 1.2371-.4459.4424-1.2588.4395-1.2588.4395 0 0-.8129.0034-1.2588-.4395-.6311-.6267.0478-1.2371.0478-1.2371l.805-.8371-1.0691-1.0632.841-.0084.6336.6464z&quot;,<br>      &quot;size&quot;: 300,<br>      &quot;filled&quot;: true,<br>      &quot;strokeWidth&quot;: 0,<br>      &quot;opacity&quot;: 1<br>    }<br>}</pre><p>In context, once you have multiple SVGs, we have to simulate ROW CONTEXT, with a series of conditional expressions (IF statements) to ensure the correct SVG appears alongside the correct attribute. Additionally, we use the same expressions to colour the SVGs accordingly.</p><p>With the Params method, we can create one long conditional expression to display all the required SVG marks.</p><pre>// params method in Deneb<br>&quot;layer&quot;:[{<br>  {<br>    &quot;mark&quot;: {<br>      &quot;type&quot;: &quot;point&quot;,<br>      &quot;fill&quot;: {<br>        &quot;expr&quot;: &quot;datum.party == &#39;LAB&#39; ? &#39;#FFF&#39; : datum.party == &#39;SF&#39; ? &#39;rgba(36,170,131,255)&#39; : datum.party == &#39;CON&#39; ? &#39;rgba(5,117,201,255)&#39; : datum.party == &#39;LD&#39; ? &#39;rgb(251,177,20)&#39; : datum.party == &#39;OTH&#39; ? &#39;rgba(186,186,186,255)&#39; : datum.party == &#39;SNP&#39; ? &#39;rgba(255,208,44,255)&#39; : &#39;&#39; &quot;<br>      },<br>  <br>      &quot;shape&quot;: {<br>        &quot;expr&quot;: &quot;datum.party == &#39;LAB&#39; ? pLAB : datum.party == &#39;SF&#39; ? pSF : datum.party == &#39;CON&#39; ? pCON : datum.party == &#39;LD&#39; ? pLD : datum.party == &#39;OTH&#39; ? pOTH : datum.party == &#39;SNP&#39; ? pSNP : &#39;&#39; &quot;<br>      },<br>      &quot;opacity&quot;: 1,<br>      &quot;filled&quot;: true,<br>      &quot;size&quot;: 300<br>    }<br>  }<br>}]</pre><p>With the Styles method, we can’t use expressions. Instead, we create a mark for each SVG separately:</p><pre>// styles method<br>&quot;layer&quot;: [{<br>  &quot;mark&quot;: {<br>    &quot;type&quot;: &quot;point&quot;,<br>    &quot;shape&quot;: &quot;circle&quot;,<br>    &quot;fill&quot;: &quot;black&quot;,<br>    &quot;opacity&quot;: 1,<br>    &quot;filled&quot;: true,<br>    &quot;size&quot;: {<br>      &quot;expr&quot;: &quot;datum.party == &#39;SNP&#39; ? 2500 : 0&quot;<br>    }<br>  },<br>  &quot;encoding&quot;: {}<br>},<br>{<br>  &quot;mark&quot;: {<br>    &quot;type&quot;: &quot;point&quot;,<br>    &quot;style&quot;: &quot;snp&quot;,<br>    &quot;size&quot;: {<br>      &quot;expr&quot;: &quot;datum.party == &#39;SNP&#39; ? 300 : 0&quot;<br>    },<br>    &quot;stroke&quot;: &quot;black&quot;<br>  },<br>  &quot;encoding&quot;: {}<br>}]</pre><p>The result is something wonderful indeed 🥹</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/766/1*mqu3bm0ff-NITgMzO6yB9g.png" /></figure><p>I hope you found this article useful, I apologise I couldn’t make it more detail. In the interest of time, I’ve kept it as succinct as possible. You can find the PBIX below!</p><p>🔗Github Repo: <a href="https://github.com/PBI-DataVizzle/pbi_content/tree/main/my_deneb_showcases/svg">Deneb Showcase — SVGs Part2</a></p><p>#StayQueryous #PBIDataVizzle</p><h3>Better Resources:</h3><ul><li><a href="https://hatfullofdata.blog/svg-in-power-bi-part-1/">Laura Graham Brown (Hat Full of Data) | SVGs in Power BI</a></li><li><a href="https://kerrykolosko.com/category/dataviz/svg/">Kerry Kolosko | SVG Archive — Custom Visuals in Power BI</a></li><li><a href="https://medium.com/the-bi-corner/step-up-your-power-bi-game-with-svgs-e0e255c1316d">Isabelle Bittar | Step Up Your Game with SVGs in Power BI</a></li><li><a href="https://www.youtube.com/watch?v=wc8jlKHTyYI&amp;ab_channel=Fernan">Solutions Aboard (Fernan) | Custom Visuals with SVGs</a></li><li><a href="https://www.sqlbi.com/articles/creating-custom-visuals-in-power-bi-with-dax/">Kurt Buhler (SQLBI) | SVG Custom Visuals in Power BI</a></li><li><a href="https://github.com/PBI-David/SVG">David Bacci | SVG Chips and Donuts</a></li><li><a href="https://www.youtube.com/watch?v=QOcP5OvSwlI&amp;ab_channel=HowtoPowerBI">Bas Dohemn (How to Power BI) | Building SVG Charts in Power BI</a></li><li><a href="https://www.havensconsulting.net/blog-and-media/tag/SVG">Havens Consulting | SVG Topics</a></li><li><a href="https://workout-wednesday.com/pbi-2024-w11/">Injae Park | IBCS-style Charts with SVGs (WorkOut Wednesday)</a></li><li><a href="https://www.powerofbi.org/youtube/">Avatorl (Andrzej Leszkiewicz) | Data Visualisation and Beyond</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NuxpYOdU5Q0Y2vQUIHbT1g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VIlmX1inzQbgF0RRm028lg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*qA2vYpxfKRzkve3AKP37iw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6bb623b98de2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deneb & Vega-Lite and PBI Core Visuals | SVG Images (Part 1)]]></title>
            <link>https://pbi-datavizzle.medium.com/deneb-vega-lite-and-pbi-core-visuals-svg-images-part-1-9e3d8cd77351?source=rss-23e0d5d396ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e3d8cd77351</guid>
            <category><![CDATA[svg]]></category>
            <category><![CDATA[vega-lite]]></category>
            <category><![CDATA[dataviz]]></category>
            <category><![CDATA[power-bi]]></category>
            <category><![CDATA[data-visualization]]></category>
            <dc:creator><![CDATA[PBI DataVizzle]]></dc:creator>
            <pubDate>Tue, 31 Dec 2024 11:22:56 GMT</pubDate>
            <atom:updated>2025-02-01T17:01:35.946Z</atom:updated>
            <content:encoded><![CDATA[<h4>Several methods on how to implement SVG Images and SVG Paths in both Core Visuals and Deneb (Vega-Lite)🕊️ 🧙🏼‍♂️ ✨</h4><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*ZbgDarqyTp_GPr93Ot9kkA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*eUvKzwolk68J1xzg55OjMg.png" /></figure><p>💌 <em>PBIX file available at the end of the article… Enjoy!</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AgOdwohrtLArrvumkl1Q5Q.png" /></figure><h3>Introduction:</h3><p>SVGs (Scalable Vector Graphics) provide a light-weight, flexible solution to further finesse or “beautify” data storytelling and #DataViz design in your Power BI reports. The beauty of SVGs compared to PNGs is that SVGs scale (resize) without pixelation or distortion.</p><p>In this article I will show you a basic working example of static SVG images using both Power BI Core Visuals (Table visual) and Deneb (Vega-Lite).</p><p><strong><em>Important Note: </em></strong><em>There is a lot of good quality literature on this topic by our friends in the PBI Community, much more detailed and comprehensive than I provide here🤓I’ve included a list of resources and links at the end of article🪄🫶🏽</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/328/1*DmZVXgBtoCZ_WORtfmCpjA.gif" /><figcaption>SVG = Scalable Vector Graphics</figcaption></figure><h4>First things first. Copy your image as SVG code:</h4><p>A simple-ish way to get the SVG code necessary to build our visual is to use Figma.</p><ol><li>Import or paste your image into Figma</li><li>Right click on the image, select <strong>Copy/Paste As</strong>, then <strong>Copy as SVG</strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*msbMmpiExIy-Ko-Jy9-m_A.png" /><figcaption>Copy As SVG</figcaption></figure><p>3. Paste into your preferred IDE / Text Editor</p><p>Pasting your SVG code should look something similar to this. The key observations are the ‘blocks’ &lt;svg , &lt;/svg&gt; and &lt;path . These paths are the coordinates which plot out your SVG lines.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U7hme0X0eiTtuC4IgRS42w.png" /><figcaption>SVG code in Notepad++</figcaption></figure><p>The SVG code can look difficult to read, but if we break it down into chunks, you will see it’s quite structured:</p><pre>//**ILLUSTRATIVE CODE**\\<br>//---------------------\\<br><br>// SVG START BLOCK<br>&lt;svg <br><br>// SVG HEADER<br>width=&quot;122&quot; height=&quot;122&quot; viewBox=&quot;0 0 122 122&quot; fill=&quot;none&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;<br><br>// SVG PATH 1<br>&lt;path d=&quot;M61 122C94.6894 122 122 94.6894 122 61C122 27.3106 94.6894 0 61 0C27.3106 0 0 27.3106 0 61C0 94.6894 27.3106 122 61 122Z&quot; fill=&quot;#FFD02C&quot;/&gt;<br><br>// SVG PATH 2<br>&lt;path d=&quot;M61.05 92.42C61.05 92.42 69.8501 92.71 76.2701 86.57C82.69 80.43 79.41 74.15 77.98 72.3C76.56 70.45 61.0399 54.9 61.0399 54.9C61.0399 54.9 45.53 70.45 44.11 72.3C42.68 74.16 39.4 80.43 45.82 86.57C52.24 92.7 61.0399 92.42 61.0399 92.42M61.0399 37.73L73.9001 24.61L90.98 24.78L69.2801 46.36L85.63 63.35C85.63 63.35 99.4 75.74 86.6 88.46C77.55 97.44 61.05 97.38 61.05 97.38C61.05 97.38 44.55 97.45 35.5 88.46C22.69 75.74 36.47 63.35 36.47 63.35L52.81 46.36L31.11 24.78L48.18 24.61L61.0399 37.73Z&quot; fill=&quot;black&quot;/&gt;<br><br>// SVG END BLOCK<br>&lt;/svg&gt;</pre><p>4. Make the SVG Code workable in DAX</p><p>Once you’ve pasted your SVG code into your preferred text editor, save the file with the .svg extension. We will need this file again shortly.</p><p>Now, if you were to simply copy and paste the SVG code in its current form, you’ll notice a few problems:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gdSnaUy7ndey63lViO3aFQ.png" /><figcaption>SVG (not working)</figcaption></figure><p>Notice the squiggly red lines? This is bad news, we don’t want the squigglies. We are getting these because of the double-quotations / speech marks (“”). We need to replace the “” with ‘’ first, then wrap the whole SVG code within the speech marks. So you’ll end up with a clean DAX measure and SVG Code as a complete text string:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m6q_cH2K2Nt1N0dtkM_oRw.png" /></figure><p>5. Set SVG Measure to Data Category “Image URL”</p><p>In order for our measure to behave properly, we need to let it know that it not a normal text string but an image URL text string. We navigate to the “Measure Tools” ribbon and select “Image URL” from the Data Category dropdown list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*RMsIYoFbl3PX-v2CHgQd0w.png" /></figure><p>6. Final Touch, add prefix to make SVG render correctly</p><p>In order for our SVG to render correctly, we need to include additional text at the beginning of the code. The prefix looks like this:</p><pre>&quot;data:image/svg+xml;utf8, &quot;</pre><p>In context, it goes before the starting &lt;svg block:</p><pre>&quot;<br>data:image/svg+xml;utf8, &lt;svg width=&#39;122&#39; height=&#39;122&#39; viewBox=&#39;0 0 122 122&#39; fill=&#39;none&#39; xmlns=&#39;http://www.w3.org/2000/svg&#39;&gt;<br>&lt;path d=&#39;M61 122C94.6894 122 122 94.6894 122 61C122 27.3106 94.6894 0 61 0C27.3106 0 0 27.3106 0 61C0 94.6894 27.3106 122 61 122Z&#39; fill=&#39;#FFD02C&#39;/&gt;<br>&lt;path d=&#39;M61.05 92.42C61.05 92.42 69.8501 92.71 76.2701 86.57C82.69 80.43 79.41 74.15 77.98 72.3C76.56 70.45 61.0399 54.9 61.0399 54.9C61.0399 54.9 45.53 70.45 44.11 72.3C42.68 74.16 39.4 80.43 45.82 86.57C52.24 92.7 61.0399 92.42 61.0399 92.42M61.0399 37.73L73.9001 24.61L90.98 24.78L69.2801 46.36L85.63 63.35C85.63 63.35 99.4 75.74 86.6 88.46C77.55 97.44 61.05 97.38 61.05 97.38C61.05 97.38 44.55 97.45 35.5 88.46C22.69 75.74 36.47 63.35 36.47 63.35L52.81 46.36L31.11 24.78L48.18 24.61L61.0399 37.73Z&#39; fill=&#39;black&#39;/&gt;<br>&lt;/svg&gt;<br>&quot;</pre><p>See how the image does not render without the prefix:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/822/1*QmpXszcg3udpU2e2V4VPNQ.png" /></figure><p>7. Some alternative tools to improve the above process</p><p>As you will have no doubt already noticed, setting up SVGs can be quite onerous, it takes quite a bit of effort and manual interventions to get right.</p><p>There is one tool I stumbled across which may speed things up a bit. Created by Carlos Bergamo, this online tool converts your raw SVG code to a DAX-ready format. How does it work, easy</p><ul><li>Go to <a href="https://carlosbergamo.github.io/SVG-TO-PBI/">SVG to PBI</a> website</li><li>Remember the SVG file we saved earlier? Choose that file to upload</li><li>Hit “<em>Convertir</em>” (<em>Convert </em>in English)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/943/1*CmKomd1h3SbRz-HYtamZVg.png" /></figure><ul><li>Copy (Copiar) the generated text and paste into your new SVG measure 🧙🏼‍♂️ Done!</li></ul><p>8. Created a SELECTEDVALUE() measure to contain your SVGs</p><p>Using SELECTEDVALUE() allows us to create row context and assign specific SVG measures to specific rows in our table. Once you’ve created your SVG measures individual, contain them in a single measure with the SWITCH() function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*T_FdZmMYp9tXTGihKZdUSg.png" /></figure><p>This article got really long really quick, so I’ve split it into two parts. In Part 2, we will look at using SVGs with Deneb and Vega-Lite… see you there!</p><p>#StayQueryous</p><p>🔗Github Repo: <a href="https://github.com/PBI-DataVizzle/pbi_content/tree/main/my_deneb_showcases/svg">Deneb Showcase — SVGs Part</a>1</p><h3>Better Resources:</h3><ul><li><a href="https://hatfullofdata.blog/svg-in-power-bi-part-1/">Laura Graham Brown (Hat Full of Data) | SVGs in Power BI</a></li><li><a href="https://kerrykolosko.com/category/dataviz/svg/">Kerry Kolosko | SVG Archive — Custom Visuals in Power BI</a></li><li><a href="https://medium.com/the-bi-corner/step-up-your-power-bi-game-with-svgs-e0e255c1316d">Isabelle Bittar | Step Up Your Game with SVGs in Power BI</a></li><li><a href="https://www.youtube.com/watch?v=wc8jlKHTyYI&amp;ab_channel=Fernan">Solutions Aboard (Fernan) | Custom Visuals with SVGs</a></li><li><a href="https://www.sqlbi.com/articles/creating-custom-visuals-in-power-bi-with-dax/">Kurt Buhler (SQLBI) | SVG Custom Visuals in Power BI</a></li><li><a href="https://github.com/PBI-David/SVG">David Bacci | SVG Chips and Donuts</a></li><li><a href="https://www.youtube.com/watch?v=QOcP5OvSwlI&amp;ab_channel=HowtoPowerBI">Bas Dohemn (How to Power BI) | Building SVG Charts in Power BI</a></li><li><a href="https://www.havensconsulting.net/blog-and-media/tag/SVG">Havens Consulting | SVG Topics</a></li><li><a href="https://workout-wednesday.com/pbi-2024-w11/">Injae Park | IBCS-style Charts with SVGs (WorkOut Wednesday)</a></li><li><a href="https://www.powerofbi.org/youtube/">Avatorl (Andrzej Leszkiewicz) | Data Visualisation and Beyond</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*URMQxNOaWlLwolWdwTjv9Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*qA2vYpxfKRzkve3AKP37iw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e3d8cd77351" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL SERVER: Dynamic Pivot & Unpivot Techniques]]></title>
            <link>https://pbi-datavizzle.medium.com/sql-server-dynamic-pivot-unpivot-techniques-9b1913fa4b3e?source=rss-23e0d5d396ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b1913fa4b3e</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[data-transformation]]></category>
            <category><![CDATA[etl]]></category>
            <category><![CDATA[sql-server]]></category>
            <category><![CDATA[power-query]]></category>
            <dc:creator><![CDATA[PBI DataVizzle]]></dc:creator>
            <pubDate>Sat, 26 Oct 2024 17:30:50 GMT</pubDate>
            <atom:updated>2024-10-26T19:43:27.982Z</atom:updated>
            <content:encoded><![CDATA[<h4>Bringing Power Query-esque dynamic (un)pivot functionality to SQL🕊️ 🧙🏼‍♂️ ✨</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/0*U6GitSKc9w4L3voL.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bostVdrnmM8oqeQfWusjiw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*ssocq2voYoIb5PFv" /></figure><h3>TL;DR</h3><p>Unlock powerful SQL techniques to dynamically pivot and unpivot your tables. See how this compares with Power Query.</p><h4>Introduction</h4><p>The relationship between SQL and Power BI cannot be understated, it is a match made in Data Heaven.</p><p>As a Power BI and Power Query addict, I find great joy in Power Query in that you can very quickly and easily pivot and unpivot your tables. Even better, you can unpivot OTHER columns, which let’s you choose the columns you want to keep and dynamically unpivot the remain other columns.</p><p>However, as I now swim in the SQL waters without any armbands, I am fast discovering that SQL is shark infested 🦈 with no helpful GUI buttons to aid our immersion.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*S2T-rWyKBxEjRXBEbFImHw.gif" /><figcaption>PoV: SQL n00b #Mood</figcaption></figure><p>For this reason, I tried to replicate one of the most powerful ETL techniques for Data Analysts , Data Engineers, Data Scientists, Data Enthusiast and Data Everyone… these techniques are PIVOT and UNPIVOT!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*45kMGGgA_E07E0SLOmePPg.gif" /></figure><h3>STEP 1: Create SQL Tables</h3><p>We’ll create two small datasets. You can copy and paste these to your SQL Server DB and Execute them directly.</p><h4>1. Data to Unpivot</h4><pre>-- Step 1: Drop the table if it exists to avoid duplication<br>IF OBJECT_ID(&#39;dbo.product_sales&#39;, &#39;U&#39;) IS NOT NULL DROP TABLE dbo.product_sales;<br><br>-- Step 2: Create the product_sales table with product info and monthly sales columns<br>CREATE TABLE dbo.dummy_product_sales (<br>    ProductID INT PRIMARY KEY,                 -- Unique identifier for each product<br>    ProductCategoryID INT,                     -- Identifier for product category<br>    ProductCategoryName NVARCHAR(50),          -- Name of the product category<br>    ProductName NVARCHAR(100),                 -- Name of the product<br><br>    -- Monthly sales columns from January to December 2024<br>    [2024-01-01] INT,<br>    [2024-02-01] INT,<br>    [2024-03-01] INT,<br>    [2024-04-01] INT,<br>    [2024-05-01] INT,<br>    [2024-06-01] INT,<br>    [2024-07-01] INT,<br>    [2024-08-01] INT,<br>    [2024-09-01] INT,<br>    [2024-10-01] INT,<br>    [2024-11-01] INT,<br>    [2024-12-01] INT<br>);<br><br>-- Step 3: Insert the provided data into the product_sales table<br>INSERT INTO dbo.dummy_product_sales (ProductID, ProductCategoryID, ProductCategoryName, ProductName,<br>    [2024-01-01], [2024-02-01], [2024-03-01], [2024-04-01], [2024-05-01], [2024-06-01],<br>    [2024-07-01], [2024-08-01], [2024-09-01], [2024-10-01], [2024-11-01], [2024-12-01]<br>)<br>VALUES<br>    (1, 1, &#39;Bikes&#39;, &#39;Mountain Bike&#39;, 599, 207, 877, 428, 786, 485, 700, 154, 168, 277, 595, 140),<br>    (2, 1, &#39;Bikes&#39;, &#39;Road Bike&#39;, 473, 632, 840, 231, 502, 395, 890, 464, 981, 304, 482, 900),<br>    (3, 1, &#39;Bikes&#39;, &#39;Hybrid Bike&#39;, 497, 951, 430, 560, 868, 345, 118, 607, 287, 145, 898, 244),<br>    (4, 2, &#39;Components&#39;, &#39;Brakes&#39;, 541, 866, 268, 116, 697, 459, 456, 870, 393, 156, 989, 106),<br>    (5, 2, &#39;Components&#39;, &#39;Wheels&#39;, 369, 889, 752, 566, 531, 882, 943, 211, 751, 842, 137, 125),<br>    (6, 2, &#39;Components&#39;, &#39;Saddles&#39;, 935, 762, 661, 757, 797, 440, 389, 215, 211, 122, 344, 607),<br>    (7, 3, &#39;Clothing&#39;, &#39;Jersey&#39;, 246, 568, 263, 466, 366, 647, 258, 323, 581, 636, 542, 667),<br>    (8, 3, &#39;Clothing&#39;, &#39;Gloves&#39;, 449, 996, 653, 455, 599, 306, 936, 999, 120, 382, 784, 170),<br>    (9, 3, &#39;Clothing&#39;, &#39;Helmet&#39;, 607, 196, 382, 195, 345, 474, 684, 655, 974, 162, 236, 718),<br>    (10, 4, &#39;Accessories&#39;, &#39;Water Bottle&#39;, 479, 394, 798, 755, 705, 942, 202, 862, 224, 103, 484, 631),<br>    (11, 4, &#39;Accessories&#39;, &#39;Lights&#39;, 661, 459, 362, 750, 705, 590, 940, 435, 701, 278, 584, 356),<br>    (12, 4, &#39;Accessories&#39;, &#39;Lock&#39;, 875, 178, 526, 476, 291, 929, 884, 857, 834, 388, 274, 933);<br><br>-- Step 4: Verify the inserted data<br>SELECT * FROM dbo.dummy_product_sales;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9mEyF_J2c7alS_jKZWok5Q.png" /></figure><h4>2. Data to Pivot</h4><pre>-- Step 1: Drop the table if it exists to avoid duplication<br>IF OBJECT_ID(&#39;dbo.product_catalog&#39;, &#39;U&#39;) IS NOT NULL DROP TABLE dbo.product_catalog;<br><br>-- Step 2: Create the product_catalog table with specified columns<br>CREATE TABLE dbo.data_to_pivot (<br>    ProductID INT PRIMARY KEY,                -- Unique identifier for each product<br>    ProductCategoryID INT,                    -- Identifier for product category<br>    ProductCategoryName NVARCHAR(50),         -- Name of the product category<br>    ProductName NVARCHAR(100),                -- Name of the product<br>    Quantity INT                              -- Quantity available for each product<br>);<br><br>-- Step 3: Insert predefined data into the product_catalog table<br>INSERT INTO dbo.data_to_pivot (ProductID, ProductCategoryID, ProductCategoryName, ProductName, Quantity)<br>VALUES<br>    (1, 1, &#39;Bikes&#39;, &#39;Mountain Bike&#39;, 10),<br>    (2, 1, &#39;Bikes&#39;, &#39;Road Bike&#39;, 5),<br>    (3, 1, &#39;Bikes&#39;, &#39;Hybrid Bike&#39;, 50),<br>    (4, 2, &#39;Components&#39;, &#39;Brakes&#39;, 200),<br>    (5, 2, &#39;Components&#39;, &#39;Wheels&#39;, 150),<br>    (6, 2, &#39;Components&#39;, &#39;Saddles&#39;, 350),<br>    (7, 3, &#39;Clothing&#39;, &#39;Jersey&#39;, 65),<br>    (8, 3, &#39;Clothing&#39;, &#39;Gloves&#39;, 50),<br>    (9, 3, &#39;Clothing&#39;, &#39;Helmet&#39;, 40),<br>    (10, 4, &#39;Accessories&#39;, &#39;Water Bottle&#39;, 500),<br>    (11, 4, &#39;Accessories&#39;, &#39;Lights&#39;, 350),<br>    (12, 4, &#39;Accessories&#39;, &#39;Lock&#39;, 850);<br><br>-- Step 4: Verify the inserted data<br>SELECT * FROM dbo.data_to_pivot;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*c_3ffp3E1hoALdP9NioRog.png" /></figure><h3>STEP 2: SQL Transformations</h3><p>Here I will show you both the standard ‘hardcoded’ way, and then the better more ‘dynamic’ way.</p><h4>Hardcoded Pivot</h4><p>Here we will build our code. We are using CTEs because they help us break our code down into logical steps (similar to Power Query steps… kind of). CTEs also allow us to build our ETL in layers, enabling us to create more complex transformations.</p><p>The key component in the code below is Part 2a &amp; 2b, where we define how we want the pivot table to look like AFTER the pivot is complete.</p><p>Below we can see:</p><ol><li>[Big] , [Small]</li><li>FOR [SIZE] IN ( [Big] , [Small] )</li></ol><p>These are our:</p><ol><li>Pivotted Values</li><li>New Pivotted Columns</li></ol><pre>-- part 2a: define post-pivot table<br>, cte_pivot_table AS (       -- post-pivot table<br>    SELECT <br>   [ProductCategoryName],   -- column to keep<br>   [Big],                   -- ProductSizeValues (will make dynamic later)<br>   [Small]                  -- ProductSizeValues (will make dynamic later)<br><br>    FROM cte_raw_data_to_pivot<br><br>-- part2b: define pivot table properties<br>    PIVOT (                 -- pivot table operation<br>        SUM([Quantity])     -- pivot values<br>        FOR [Size] IN (     -- pivot attributes<br>    [Big],                  -- attribute values (will make dynamic later)<br>    [Small]                 -- attribute values (will make dynamic later)<br>   )<br>    ) AS piv_tbl            -- pivot table name<br>)</pre><p>Here is the full script below:</p><pre><br>--====== part 0: query imported table<br>WITH cte_raw_table AS (<br>    SELECT * FROM [Test_DB].[dbo].[data_to_pivot]<br>)<br><br>--====== part 1: define pre-pivot table<br>, cte_raw_data_to_pivot AS (   <br>    SELECT        -- RAW DATA TO PIVOT<br>        [ProductCategoryName],    -- column to keep<br>        [Size],                   -- column to pivot<br>        [Quantity]                -- pivot value<br>    FROM cte_raw_table<br>) <br><br>--====== part 2a: define post-pivot table<br>, cte_pivot_table AS (     -- post-pivot table<br>    SELECT <br>        [ProductCategoryName],-- column to keep<br>   [Big],                     -- ProductSizeValues (will make dynamic later)<br>   [Small]                    -- ProductSizeValues (will make dynamic later)<br><br>    FROM cte_raw_data_to_pivot<br><br>--====== part2b: define pivot table properties<br>    PIVOT (                 -- pivot table operation<br>        SUM([Quantity])     -- pivot values<br>        FOR [Size] IN (     -- pivot attributes<br>              [Big],        -- attribute values (will make dynamic later)<br>              [Small]       -- attribute values (will make dynamic later)<br>           )<br>    ) AS piv_tbl            -- pivot table name<br>)<br>SELECT * FROM cte_pivot_table;<br>SELECT * FROM [Test_DB].[dbo].[data_to_pivot];</pre><p>The challenge is to make the fields in 1. and 2. dynamic, so we don’t need to physically type them (hardcode them) into our script.</p><h4>Dynamic Pivot</h4><p>There are several parts to make these dynamic. It looks like a lot of code, but really, we are creating just a few variables and one more function called STRING_AGG(). In Power Query we have a similar function in the shape of Text.Combine() .</p><p>Returning to Parts 2a and 2b, we replace our hardcoded fields with dynamically concatenated list of fields, stored as a text string:</p><pre>--======================<br>-- DYNAMIC PIVOT TABLE<br>--======================<br>DECLARE @DynamicSQL NVARCHAR(MAX);  -- variable to capture dynamic SQL Query script<br>DECLARE @pivot_attributes NVARCHAR(MAX); -- varaible to capture pivot attribute values</pre><pre>-- Step 1: Build a comma-separated list of unique sizes to pivot<br>WITH cte_pivot_attributes AS (<br> SELECT DISTINCT <br>            [Size]        -- column with pivot attributes<br>        FROM [Test_DB].[dbo].[data_to_pivot] -- source table<br>        WHERE <br>            [Size] IS NOT NULL     -- remove nulls<br>)<br>SELECT <br>   @pivot_attributes = STRING_AGG(&#39;[&#39; + [Size] + &#39;]&#39;, &#39;, &#39;) -- column to extract attributes<br>FROM cte_pivot_attributes;<br><br>-- Debug: Check the generated pivot columns<br>PRINT &#39;Pivot Columns Attributes: &#39; +@pivot_attributes;</pre><p>We can print the result out which looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/481/1*7NCi84tz_898s8nyLYcHoA.png" /></figure><p>And now for the slightly unorthodox approach… rather than write our script out as we normally would, we will code our script as text string stored in a variable, we will then inject our &#39;@pivot_attributes&#39; variable containing our list of pivot attributes directly into the SQL text string.</p><pre>SET @DynamicSQL = &#39;<br><br><br>    -- Part 1: define pre-pivot table<br>    WITH cte_raw_data_to_pivot AS (   <br>        SELECT        <br>            [ProductCategoryName],   <br>            [Size],       <br>            [Quantity]      <br>        FROM [Test_DB].[dbo].[data_to_pivot]<br>    ),<br><br>    -- Part 2a: define post-pivot table<br>    cte_pivot_table AS (<br>        SELECT <br>            [ProductCategoryName], &#39; +@pivot_attributes + &#39; -- Dynamically include pivot columns<br>        FROM cte_raw_data_to_pivot<br><br> -- Part 2b: define pivot table<br>        PIVOT (        <br>            SUM([Quantity]) -- pivot values<br>            FOR [Size] IN (&#39; +@pivot_attributes + &#39;)  -- Use dynamic list<br>        ) AS piv_tbl<br>    )<br>    SELECT * FROM cte_pivot_table;&#39;;    -- final output<br><br>-- Debug: Print the generated SQL to verify<br>PRINT &#39;Generated SQL: &#39; + @DynamicSQL;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/1*UJu-Lpgzp1UNbyY4H-_wzg.png" /></figure><p>We can print out the resulting output, which looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/863/1*ZX4SKrlDedD_sI3f-YOncA.png" /></figure><p>And now, because we have stored this as a string in a variable, we can execute the string as a SQL statement:</p><pre>-- Step 3: Execute the dynamic SQL<br>EXEC sp_executesql @DynamicSQL;</pre><p>And the result is magic… from this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/407/1*lIRHOv-D7Fg0K64za_KP2A.png" /><figcaption>before pivot</figcaption></figure><p>… to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/241/1*3BKUMTQvDeOflF3pan-52w.png" /><figcaption>after pivot</figcaption></figure><p>And the full dynamic pivot script in context:</p><pre><br>--======================<br>-- DYNAMIC PIVOT TABLE<br>--======================<br>DECLARE @DynamicSQL NVARCHAR(MAX);  -- variable to capture dynamic SQL Query script<br>DECLARE @pivot_attributes NVARCHAR(MAX); -- varaible to capture pivot attribute values<br><br>--==== Step 1: Build a comma-separated list of unique sizes to pivot<br>WITH cte_pivot_attributes AS (<br> SELECT DISTINCT <br>            [Size]                           -- column with pivot attributes<br>        FROM [Test_DB].[dbo].[data_to_pivot] -- source table<br>        WHERE <br>            [Size] IS NOT NULL               -- remove nulls<br>)<br>SELECT <br>   @pivot_attributes = <br>      STRING_AGG(&#39;[&#39; + [Size] + &#39;]&#39;, &#39;, &#39;)   -- column to extract attributes<br>FROM cte_pivot_attributes;<br><br>--==== Debug: Check the generated pivot columns<br>PRINT &#39;Pivot Columns Attributes: &#39; +@pivot_attributes;<br><br>--==== Step 2: Construct the dynamic SQL; <br>--==== replace attributes with @pivot_attributes<br>SET @DynamicSQL = &#39;<br>    -- Part 0: query imported table<br>    WITH cte_raw_table AS (<br>        SELECT * FROM [Test_DB].[dbo].[data_to_pivot]<br>    ),<br><br>    -- Part 1: define pre-pivot table<br>    cte_raw_data_to_pivot AS (   <br>        SELECT        <br>            [ProductCategoryName],   <br>            [Size],       <br>            [Quantity]      <br>        FROM cte_raw_table<br>    ),<br><br>    -- Part 2a: define post-pivot table<br>    cte_pivot_table AS (<br>        SELECT <br>            [ProductCategoryName], &#39; +@pivot_attributes + &#39; -- Dynamically include pivot columns<br>        FROM cte_raw_data_to_pivot<br><br> -- Part 2b: define pivot table<br>        PIVOT (        <br>            SUM([Quantity]) -- pivot values<br>            FOR [Size] IN (&#39; +@pivot_attributes + &#39;)  -- Use dynamic list<br>        ) AS piv_tbl<br>    )<br>    SELECT * FROM cte_pivot_table;&#39;;    -- final output<br><br>--==== Debug: Print the generated SQL to verify<br>PRINT &#39;Generated SQL: &#39; + @DynamicSQL;   <br><br>--==== Step 3: Execute the dynamic SQL<br>EXEC sp_executesql @DynamicSQL;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/1*qclxmm91XhvwHdeeX3fnhg.png" /></figure><p>Similarly for Unpivot actions, we will use STRING_AGG() to get a list of columns. We’ll then filter this list to keep the columns we aren’t changing, leaving the remaining columns of the list to be unpivoted.</p><p>So instead of this:</p><pre>-- part 1: define pre-pivot table<br>cte_table_to_unpivot AS(<br>SELECT <br>    ProductCategoryName,    -- columns to keep<br>    ProductName,      -- columns to keep<br>    ActivityDate,      -- new attribute<br>    Revenue       -- new value<br>FROM cte_raw_table<br><br>-- part 2: define post-pivot table<br>UNPIVOT ( -- attribute values<br>    Revenue FOR ActivityDate IN ( -- unpivot columns (will make dynamic later)<br>       [2024-01-01]<br>      ,[2024-02-01]<br>      ,[2024-03-01]<br>      ,[2024-04-01]<br>      ,[2024-05-01]<br>      ,[2024-06-01]<br>      ,[2024-07-01]<br>      ,[2024-08-01]<br>      ,[2024-09-01]<br>      ,[2024-10-01]<br>      ,[2024-11-01]<br>      ,[2024-12-01]<br>    )<br>) AS UnpivotedTable<br>)<br>-- final output<br>SELECT * FROM cte_table_to_unpivot<br>ORDER BY ProductCategoryName, ActivityDate;</pre><p>… we do this:</p><pre>--======================<br>-- DYNAMIC UNPIVOT TABLE<br>--======================<br>DECLARE @DynamicSQL NVARCHAR(MAX);<br>DECLARE @columns_to_unpivot NVARCHAR(MAX);<br><br>-- Step 1: Dynamically build a comma-separated list of date columns to unpivot<br>WITH cte_unpivot_attributes AS (<br>    <br> SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS<br>    WHERE <br>  TABLE_NAME = &#39;data_to_unpivot&#39;         -- Table name in [dbo] schema<br>        AND COLUMN_NAME NOT IN (<br>   &#39;ProductCategoryName&#39;, <br>   &#39;ProductName&#39;, <br>   &#39;ProductId&#39;, <br>   &#39;ProductCategoryId&#39;<br>   ) -- Exclude non-date columns<br>)<br>SELECT <br>    @columns_to_unpivot = STRING_AGG(&#39;[&#39; + COLUMN_NAME + &#39;]&#39;, &#39;, &#39;)<br>FROM cte_unpivot_attributes;<br><br>-- Debug: Check the generated pivot columns<br>PRINT &#39;Unpivot Columns: &#39; + @columns_to_unpivot;</pre><p>… which generates this list:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cLGAW_ZdxtidG5kjy2uBJg.png" /></figure><p>… which we can then use in the SQL-stored-as-text variable:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*GwHbfJ8mjwet3yFnD5Av0g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*djQqxamGiW_jh2woTQb_eA.png" /></figure><p>… and when executed:</p><pre>-- Step 4: Execute the dynamic SQL<br>EXEC sp_executesql @DynamicSQL;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/359/1*EVvmtVHtHVSCaEp6ajg0Ag.png" /></figure><p>Perfectly cleansed data!! 🔥</p><p>Here is the full harcoded and dynamic unpivot script below:</p><h4>Hardcoded Unpivot</h4><pre><br>-- part 0: query imported table<br>WITH cte_raw_table AS (<br>    SELECT * FROM [Test_DB].[dbo].[data_to_unpivot]<br>),<br><br>-- part 1: define pre-pivot table<br>cte_table_to_unpivot AS(<br>SELECT <br>    ProductCategoryName,    -- columns to keep<br>    ProductName,      -- columns to keep<br>    ActivityDate,      -- new attribute<br>    Revenue       -- new value<br>FROM cte_raw_table<br><br>-- part 2: define post-pivot table<br>UNPIVOT ( -- attribute values<br>    Revenue FOR ActivityDate IN ( -- unpivot columns (will make dynamic later)<br>       [2024-01-01]<br>      ,[2024-02-01]<br>      ,[2024-03-01]<br>      ,[2024-04-01]<br>      ,[2024-05-01]<br>      ,[2024-06-01]<br>      ,[2024-07-01]<br>      ,[2024-08-01]<br>      ,[2024-09-01]<br>      ,[2024-10-01]<br>      ,[2024-11-01]<br>      ,[2024-12-01]<br>    )<br>) AS UnpivotedTable<br>)<br>-- final output<br>SELECT * FROM cte_table_to_unpivot<br>ORDER BY ProductCategoryName, ActivityDate;</pre><h4>Dynamic Unpivot</h4><pre>--======================<br>-- DYNAMIC UNPIVOT TABLE<br>--======================<br>DECLARE @DynamicSQL NVARCHAR(MAX);<br>DECLARE @columns_to_unpivot NVARCHAR(MAX);<br><br>-- Step 1: Dynamically build a comma-separated list of date columns to unpivot<br>WITH cte_unpivot_attributes AS (<br>    <br> SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS<br>    WHERE <br>  TABLE_NAME = &#39;data_to_unpivot&#39;         -- Table name in [dbo] schema<br>        AND COLUMN_NAME NOT IN (&#39;ProductCategoryName&#39;, &#39;ProductName&#39;, &#39;ProductId&#39;, &#39;ProductCategoryId&#39;) -- Exclude non-date columns<br>)<br>SELECT <br>    @columns_to_unpivot = STRING_AGG(&#39;[&#39; + COLUMN_NAME + &#39;]&#39;, &#39;, &#39;)<br>FROM cte_unpivot_attributes;<br><br>-- Debug: Check the generated pivot columns<br>PRINT &#39;Unpivot Columns: &#39; + @columns_to_unpivot;<br><br><br><br>-- Step 3: Construct the dynamic SQL; replace attributes with @columns_to_unpivot<br>SET @DynamicSQL = &#39;<br>    <br>    SELECT <br>        ProductCategoryName,           -- columns to keep<br>        ProductName,             -- columns to keep<br>        CONVERT(DATE, ActivityDate, 120) AS ActivityDate,  -- unpivot attributes<br>        Revenue              -- unpivot value<br>    FROM <br>        dbo.data_to_unpivot <br>    UNPIVOT (<br>        Revenue FOR ActivityDate IN (&#39; + @columns_to_unpivot + &#39;)  -- attribute values<br>    ) AS UnpivotedTable<br>    ORDER BY <br>  ProductCategoryName, ActivityDate;&#39;;<br><br>-- Debug: Print the generated SQL to verify<br>PRINT &#39;Generated SQL: &#39; + @DynamicSQL;<br><br>-- Step 4: Execute the dynamic SQL<br>EXEC sp_executesql @DynamicSQL;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yZ3eB6Gj2dPbqvb1McEghg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*F2aQXiql12WuhtcosFYE_A.gif" /></figure><p>🔗<strong>Github Repo: </strong><a href="https://github.com/PBIQueryous/SQL_snippets/tree/main/techniques/pivot_unpivot"><strong>SQL Dynamic Pivot / Unpivot</strong></a></p><p>Thanks for making it this far… I hope you find it useful! Until next time 🧙🏻‍♂️</p><p>Resources:</p><p><a href="https://learn.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-ver16">STRING_AGG() function</a></p><p><a href="https://learn.microsoft.com/en-us/powerquery-m/text-combine">Text.Combine() function</a></p><p>#StayQueryous</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*URMQxNOaWlLwolWdwTjv9Q.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b1913fa4b3e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deneb & Vega-Lite Walkthrough Series | EP09: The World in 2100 (showcasing the art of the…]]></title>
            <link>https://pbi-datavizzle.medium.com/deneb-vega-lite-walkthrough-series-ep09-the-world-in-2100-showcasing-the-art-of-the-74baa48f716c?source=rss-23e0d5d396ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/74baa48f716c</guid>
            <category><![CDATA[power-bi-tutorials]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[power-bi]]></category>
            <category><![CDATA[vega-lite]]></category>
            <category><![CDATA[deneb]]></category>
            <dc:creator><![CDATA[PBI DataVizzle]]></dc:creator>
            <pubDate>Sat, 11 May 2024 22:14:40 GMT</pubDate>
            <atom:updated>2024-09-10T16:23:01.587Z</atom:updated>
            <content:encoded><![CDATA[<h3>Deneb &amp; Vega-Lite Walkthrough Series | EP09: The World in 2100 (showcasing the art of the possible)📊</h3><h4>Inspiration can come from anywhere and imitation is the sincerest form of flattery. 🕊️ 🧙🏼‍♂️ ✨</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sSXBrqj-5jq38e5qSgex_w.png" /><figcaption>One of these is a digital publication, the other is a Deneb viz… can you guess which?</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*np4uF0xj5OyC7un0SKTCMg.png" /></figure><p>💌 <em>PBIX file available at the end of the article… Enjoy!</em></p><h3>Recap</h3><ul><li><a href="https://medium.com/@pbiqueryous/power-bi-deneb-vega-lite-series-ep01-marks-and-encoding-lets-learn-to-run-before-we-fly-87f4961a2fa5">EP1</a> — Marks and Encoding</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-series-ep02-mark-types-cd7a99d4a23c">EP2</a> — Mark Types</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walk-through-series-ep03-styling-mark-properties-4dddb9e0f353">EP3</a> — Styling Mark Propreties (Part 1)</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep04-styling-mark-properties-pt2-6cafb8a2e552">EP4</a> — Styling Mark Properties (Part 2)</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep05-layers-4ab67067f717">EP5</a> — Layers (Multiple Views)</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep06-expressions-conditional-formatting-pt1-eab3ca34f0de">EP6</a> — Expressions &amp; Conditional Formatting</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep07-number-formats-5ef46090d765">EP7 </a>— Number Formatting</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep08-small-multiples-facets-0e8d5f7f42b1">EP8 </a>— Facets (Small Multiples)</li></ul><blockquote>The greatest value of a picture is when it forces us to notice what we never expected to see<br>- John W. Tukey</blockquote><h3>The Magic of Graphs</h3><p>The inspiration for this episode came from an unlikely source, outside of the world of Power BI. I stumbled across a digital graphic design publication on <a href="https://www.visualcapitalist.com/when-will-the-global-population-reach-its-peak/">Visual Capitalist</a>. It demonstrates the art of data viz, making information more accessible through stunningly thought-provoking imagery and visualisation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*kkzVqTL0nNr8CQhdnkOUIg.png" /><figcaption>Visual Capitalist</figcaption></figure><p>Of course, my initial reaction was “how I’d love to reproduce this in Power BI”, followed by instant deflation knowing that this could never be produced with PBI Core Visuals. For a split-second, I almost reconsidered my life-choices, on whether or not I should have dedicated my years to Tableau instead…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*s-gIoYmgwo4NNlTBsTniZw.gif" /></figure><p>…but after a hard look in the mirror and maybe 1 or 2 coffees later ☕… I thought to myself — “but we have Deneb… surely we can achieve this, after all — it’s just an area chart and a few lines?”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/1*IEQlSi0HGWmperf3IsLilQ.gif" /></figure><p>It turns out… I wasn’t wrong 🧙🏼‍♂️🪄</p><h3>Taking an Idea and Making it Reality</h3><p>We will tackle this as a mini-project. In doing so, we will learn many Vega-Lite techniques and methods, some of it may come easy 🤓, some of it may indeed melt your brain 🧠🤯 — but we will learn together, and I hope it will unlock new understanding, and reveal your true potential 🐛💫🦋</p><p>We have a lot of ground to cover, and it might get a little intense for a bit, so get nourished, get hydrated and get on it!</p><h4>Step 0: Prepare you data fields</h4><p>All measures and fields are pre-prepared in the PBIX at the end of the article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/273/1*PsHwwyd-lFzDPhQr3ciaCw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*frWDVfH0KTAwmmT28oJYfA.png" /></figure><p>The column [Total Population] is renamed UN_Historical in the Data Values bucket.</p><h4>Step 1: Laying down the foundations</h4><p>Throughout this episode, we will utilise the best of both <a href="https://vega.github.io/vega-lite/docs/spec.html"><em>specification</em></a><em> </em>AND <a href="https://vega.github.io/vega-lite/docs/config.html"><em>config</em></a><em> </em>panes to optimise our code as efficiently as possible whilst also reducing redundancy and repetition significantly.</p><p>Copy and paste the following Config and Specs as-is, we’ll elaborate on it later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*5W0umezgAOKkvIq1tdZybQ.png" /><figcaption>Config base setup</figcaption></figure><pre>{<br>  &quot;params&quot;: [<br>    {<br>      &quot;name&quot;: &quot;__background_colour&quot;,<br>      &quot;value&quot;: &quot;#faf7e6&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;__background_transparent&quot;,<br>      &quot;value&quot;: &quot;transparent&quot;<br>    }<br>  ],<br>  &quot;background&quot;: {<br>    &quot;expr&quot;: &quot;__background_transparent&quot;<br>  },<br>  &quot;view&quot;: {&quot;stroke&quot;: &quot;transparent&quot;},<br>  &quot;font&quot;: &quot;Segoe UI&quot;,<br>  &quot;axisY&quot;: {<br>    &quot;labelExpr&quot;: &quot;datum.value &gt; 10000000000 ? null : datum.value == 0 ? 0 : datum.value / 1e9 + &#39;B&#39;&quot;<br>  },<br>  &quot;line&quot;: {<br>    &quot;interpolate&quot;: &quot;monotone&quot;,<br>    &quot;stroke&quot;: &quot;#569cd7&quot;<br>  },<br>  &quot;area&quot;: {<br>    &quot;interpolate&quot;: &quot;monotone&quot;,<br>    &quot;fill&quot;: &quot;#569cd7&quot;,<br>    &quot;opacity&quot;: 1,<br>    &quot;line&quot;: {<br>      &quot;stroke&quot;: &quot;white&quot;,<br>      &quot;strokeWidth&quot;: 2,<br>      &quot;yOffset&quot;: 4<br>    }<br>  },<br>  &quot;style&quot;: {}<br>}</pre><p>The <a href="https://vega.github.io/vega-lite/docs/config.html">config</a> tab will play a central role in how we format and define our chart properties. Think of it as a theme setting. You can manage your chart and canvas theme as a separate entity. Keeping it seperate to the specification means your specification code will be much cleaner and less cluttered. Crucially, it allows us to define default global settings to each mark, keeping things consistent and concise.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*-TamJ0fcth8MjOAcOcKJzA.png" /><figcaption>Base spec</figcaption></figure><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;Date&quot;,<br>      &quot;type&quot;: &quot;temporal&quot;<br>    },<br>    &quot;y&quot;: {<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;aggregate&quot;: &quot;sum&quot;<br>    }<br>  },<br>  &quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;area&quot;,<br>        &quot;opacity&quot;: 1<br>      },<br>      &quot;encoding&quot;: {<br>        &quot;y&quot;: {<br>          &quot;field&quot;: &quot;UN_Historical&quot;<br>        }<br>      }<br>    }<br>  ]<br>}</pre><p>Now that the config is pre-defined, we can start a simple spec with some cheeky flair already in effect. The foundation is the <em>area mark. </em>The area mark is quite dexterous it than it has two components, the area and the line, and we can modify both independently. If you look closely at the original publication, there is a white line just beneath the top of the area border. We’ve fine-tuned the area properties to include a white line, with an adjusted yOffset</p><pre>//--- CONFIG ---\\<br><br>&quot;area&quot;: {                    // &lt;&lt;--- start area properties<br>    &quot;interpolate&quot;: &quot;monotone&quot;, // &lt;-- default interpolate = monotone<br>    &quot;fill&quot;: &quot;#569cd7&quot;,         // &lt;-- default fill colour<br>    &quot;opacity&quot;: 1,              // &lt;-- default opacity (transparency = 0)<br>    &quot;line&quot;: {                    // &lt;&lt;-- start line sub-attribute<br>      &quot;stroke&quot;: &quot;white&quot;,           // &lt;-- default line colour<br>      &quot;strokeWidth&quot;: 2,            // &lt;-- default line thickness<br>      &quot;yOffset&quot;: 4                 // &lt;-- default offset<br>    }                            // &lt;&lt;-- end line sub-attribute<br>  }                          // &lt;&lt;--- end area properties</pre><p>By applying our area config settings here, your spec is now much tidier:</p><pre>//--- SPEC ---\\<br>{<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;area&quot;,  // we don&#39;t need to code our attributes here<br>                         // as &quot;area&quot; properties are defined in the config<br>        &quot;opacity&quot;: 1<br>      }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MAobqlY0Qg3DSzgKmBzA0g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*kkzVqTL0nNr8CQhdnkOUIg.png" /><figcaption>Visual Capitalist</figcaption></figure><h4>Step 2: Add additional marks</h4><p>Again, one step at a time. We will take an organic approach, apply the natural next steps, and encounter real-world conudrums!</p><p>So, we want to add another line mark? Easy, yes? Yes! 🤓</p><p>Add a line mark, bind the y-axis to the field [IIASA] as follows:</p><pre>{<br>  &quot;mark&quot;: {<br>    &quot;type&quot;: &quot;line&quot;,<br>    &quot;stroke&quot;: &quot;red&quot;<br>  },<br>  &quot;encoding&quot;: {<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;IIASA&quot;,<br>      &quot;type&quot;: &quot;quantitative&quot;<br>    }<br>  }<br>}</pre><p>… run that script! Oh, what’s this? 🤔</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/873/1*H419zogj9tmctZLn2F2oWA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/399/1*HKSLDppDP7TTpKlvraAZCA.png" /></figure><p>Where is our smooth line? Can you guess what might be the cause of this issue? Correct... nulls! Luckily for us, Vega-Lite and Deneb are sublime, and we have answers to these problems.</p><p>All we need to do is filter out the nulls, or rather the opposite, filter IN the valid numbers (nulls are classed as invalid). It looks something like this:</p><pre>&quot;transform&quot;: [<br>        {                                   // filter transform<br>          &quot;filter&quot;: &quot;isValid(datum.IIASA)&quot;  // isValid() function<br>        }<br>      ]</pre><p>In context, we have this:</p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;encoding&quot;: {...},<br>  &quot;layer&quot;: [<br>    {                                  // area mark<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;area&quot;,<br>        &quot;opacity&quot;: 1<br>      },<br>      &quot;encoding&quot;: {<br>        &quot;y&quot;: {&quot;field&quot;: &quot;UN_Historical&quot;}<br>      }<br>    },<br>    {                                  // start line mark<br>      &quot;transform&quot;: [<br>        {<br>          &quot;filter&quot;: &quot;isValid(datum.IIASA)&quot;  // filter for valid values only<br>        }<br>      ],<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;line&quot;,<br>        &quot;stroke&quot;: &quot;red&quot;<br>      },<br>      &quot;encoding&quot;: {<br>        &quot;y&quot;: {&quot;field&quot;: &quot;IIASA&quot;}            // encode on y-axis<br>      }<br>    }                                  // end line mark<br>  ]<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/871/1*lD2uxSdA0QyEnxaJKjM4rw.png" /></figure><p>We can now repeat this for our remaining marks:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pWhgcBmlfyBcOTa2T8RoqQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*kkzVqTL0nNr8CQhdnkOUIg.png" /></figure><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;encoding&quot;: {...},<br>  &quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;area&quot;,<br>        &quot;opacity&quot;: 1<br>      },<br>      &quot;encoding&quot;: {<br>        &quot;y&quot;: {<br>          &quot;field&quot;: &quot;UN_Historical&quot;<br>        }<br>      }<br>    },<br>    {<br>      &quot;transform&quot;: [<br>        {<br>          &quot;filter&quot;: &quot;isValid(datum.IIASA)&quot;<br>        }<br>      ],<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;line&quot;,<br>        &quot;interpolate&quot;: &quot;monotone&quot;,<br>        &quot;stroke&quot;: &quot;red&quot;<br>      },<br>      &quot;encoding&quot;: {<br>        &quot;y&quot;: {<br>          &quot;field&quot;: &quot;IIASA&quot;<br>        }<br>      }<br>    },<br>    {<br>      &quot;transform&quot;: [<br>        {<br>          &quot;filter&quot;: &quot;isValid(datum.IHME)&quot;<br>        }<br>      ],<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;line&quot;,<br>        &quot;interpolate&quot;: &quot;monotone&quot;,<br>        &quot;stroke&quot;: &quot;yellow&quot;<br>      },<br>      &quot;encoding&quot;: {<br>        &quot;y&quot;: {<br>          &quot;field&quot;: &quot;IHME&quot;<br>        }<br>      }<br>    },<br>    {<br>      &quot;transform&quot;: [<br>        {<br>          &quot;filter&quot;: &quot;isValid(datum.Higher)&quot;<br>        }<br>      ],<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;line&quot;,<br>        &quot;interpolate&quot;: &quot;monotone&quot;,<br>        &quot;stroke&quot;: &quot;DarkBlue&quot;,<br>        &quot;strokeDash&quot;: [2, 3]<br>      },<br>      &quot;encoding&quot;: {<br>        &quot;y&quot;: {<br>          &quot;field&quot;: &quot;Higher&quot;<br>        }<br>      }<br>    },<br>    {<br>      &quot;transform&quot;: [<br>        {<br>          &quot;filter&quot;: &quot;isValid(datum.Lower)&quot;<br>        }<br>      ],<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;line&quot;,<br>        &quot;interpolate&quot;: &quot;monotone&quot;,<br>        &quot;stroke&quot;: &quot;DarkBlue&quot;,<br>        &quot;strokeDash&quot;: [2, 3]<br>      },<br>      &quot;encoding&quot;: {<br>        &quot;y&quot;: {<br>          &quot;field&quot;: &quot;Lower&quot;<br>        }<br>      }<br>    }<br>  ]<br>}</pre><h4>Step 3: Filter Area Mark</h4><p>Currently, our area mark covers all years upto 2100. But we want an area mark up to 2022 and a line mark for the remainder. Although we can’t split an area mark in half, we can create the illusion as such.</p><p>For the next step, we will modify the existing area mark, filter it for 2022 and another line mark for the same measure.</p><p><strong>In brief:</strong></p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},  // dataset<br>  &quot;encoding&quot;: {..},             // global encoding<br>  &quot;layer&quot;: [<br>    {<br>      &quot;transform&quot;: [ {&quot;filter&quot;: &quot;datum.Year &lt;= 2022&quot;} ],  // filter mark<br>      &quot;mark&quot;: { &quot;type&quot;: &quot;area&quot; },                         // area mark<br>      &quot;encoding&quot;: {&quot;y&quot;: { &quot;field&quot;: &quot;UN_Historical&quot; } }    // measure encode<br>    },<br>    { <br>      &quot;mark&quot;: { &quot;type&quot;: &quot;line&quot; },                         // line mark<br>      &quot;encoding&quot;: { &quot;y&quot;: { &quot;field&quot;: &quot;UN_Historical&quot;}}     // measure encode<br>    }     <br>  ]<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4UBvIPD5UUax-bx8mcMnHA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*kkzVqTL0nNr8CQhdnkOUIg.png" /></figure><h4>Step 4: Clean Up Grid lines</h4><p>Now for some advanced formatting, we will really start finessing our report now. We are trying to imitate the digital publication, so we want 3 ticks/labels on the y-axis and 4 ticks/labels on the x-axis.</p><p>For this step, we will modify our config:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*5W0umezgAOKkvIq1tdZybQ.png" /></figure><pre>// modify the y-axis properties<br><br>&quot;axisY&quot;: {<br>    &quot;title&quot;: &quot;&quot;,<br>    &quot;domain&quot;: false,<br>/* modified for effect - perform a transformation on the label to<br>   return a abbreviated number, formatted for billions */<br>    &quot;labelExpr&quot;: <br>          &quot;datum.value &gt; 10000000000 ? null <br>          : datum.value == 0 ? 0 <br>          : datum.value / 1e9 + &#39;B&#39;&quot;,<br>  },<br><br>// modify the x-axis properties<br><br>&quot;axisX&quot;: {<br>    &quot;title&quot;: &quot;&quot;,<br>    &quot;domain&quot;: true,          // show the x-axis baseline<br>    &quot;domainColor&quot;: &quot;black&quot;,  // x-axis baseline colour<br>    &quot;domainWidth&quot;: 3,        // x-axis baseline width<br>    &quot;tickSize&quot;: 5            // max number of ticks to show<br>  }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_UbyLSi-DT7CjCisauzHJA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*kkzVqTL0nNr8CQhdnkOUIg.png" /></figure><p>This is beginning to look really, really smart. Keep up the pace, you are doing fab!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*Nsm1Rs71S9hSaEtxabx3YA.gif" /></figure><h4>Step 5: Fix Colour Formatting (Style Properties)</h4><p>Now we are going to vastly improve our formatting techniques in a succinct and effective way. Back again to the Config. We will add several style attributes which will act as placeholders for a complete set of formatting properties which we can then apply to any mark. Observe!</p><p><strong>Style Example:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*5W0umezgAOKkvIq1tdZybQ.png" /></figure><pre>&quot;style&quot;: {                              // start style array<br>    &quot;_lineUN&quot;: {                          // open style object (named &quot;_lineUN&quot;)<br>      &quot;color&quot;: {&quot;expr&quot;: &quot;__colour_un&quot;}      // style attributes (referencing our parameter)<br>    }                                     // close style object<br>}</pre><p>And the complete list of styles</p><pre>{<br>  &quot;params&quot;: [<br>    {<br>      &quot;name&quot;: &quot;__background_colour&quot;,<br>      &quot;value&quot;: &quot;#faf7e6&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;__colour_un&quot;,<br>      &quot;value&quot;: &quot;#569cd7&quot;<br>    },<br>    {...},<br>    {...},<br>    {...},<br>    {...}<br>  ],<br>  &quot;background&quot;: {...},<br>  &quot;view&quot;: {&quot;stroke&quot;: &quot;transparent&quot;},<br>  &quot;font&quot;: &quot;DIN&quot;,<br>  &quot;axisY&quot;: {...},<br>  &quot;axisX&quot;: {...},<br>  &quot;axis&quot;: {...},<br>  &quot;line&quot;: {...},<br>  &quot;area&quot;: {...},<br>  &quot;style&quot;: {<br>    &quot;_lineUN&quot;: {<br>      &quot;color&quot;: {&quot;expr&quot;: &quot;__colour_un&quot;}<br>    },<br>    &quot;_lineIIASA&quot;: {<br>      &quot;color&quot;: {<br>        &quot;expr&quot;: &quot;__colour_IIASA&quot;<br>      }<br>    },<br>    &quot;_lineIHME&quot;: {<br>      &quot;color&quot;: {&quot;expr&quot;: &quot;__colour_IHME&quot;}<br>    },<br>    &quot;_lineLower&quot;: {<br>      &quot;color&quot;: {<br>        &quot;expr&quot;: &quot;__colour_lower&quot;<br>      },<br>      &quot;strokeDash&quot;: [2, 3]<br>    },<br>    &quot;_lineHigher&quot;: {<br>      &quot;color&quot;: {<br>        &quot;expr&quot;: &quot;__colour_higher&quot;<br>      },<br>      &quot;strokeDash&quot;: [2, 3]<br>    }<br>  }<br>}</pre><p>And now our Config looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*5W0umezgAOKkvIq1tdZybQ.png" /></figure><pre>{<br>  &quot;params&quot;: [<br>    {<br>      &quot;name&quot;: &quot;__background_colour&quot;,<br>      &quot;value&quot;: &quot;#faf7e6&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;__background_transparent&quot;,<br>      &quot;value&quot;: &quot;transparent&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;__colour_un&quot;,<br>      &quot;value&quot;: &quot;#569cd7&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;__colour_higher&quot;,<br>      &quot;value&quot;: &quot;#62B4CE&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;__colour_lower&quot;,<br>      &quot;value&quot;: &quot;#366791&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;__colour_IIASA&quot;,<br>      &quot;value&quot;: &quot;#fe666b&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;__colour_IHME&quot;,<br>      &quot;value&quot;: &quot;#efb92d&quot;<br>    }<br>  ],<br>  &quot;background&quot;: {<br>    &quot;expr&quot;: &quot;__background_transparent&quot;<br>  },<br>  &quot;view&quot;: {&quot;stroke&quot;: &quot;transparent&quot;},<br>  &quot;font&quot;: &quot;DIN&quot;,<br>  &quot;axisY&quot;: {<br>    &quot;title&quot;: &quot;&quot;,<br>    &quot;domain&quot;: false,<br>    &quot;labelExpr&quot;: &quot;datum.value == 0 ? datum.value : replace(format(datum.value,&#39;~s&#39;),&#39;G&#39;, &#39;B&#39;)&quot;<br>  },<br>  &quot;axisX&quot;: {<br>    &quot;title&quot;: &quot;&quot;,<br>    &quot;domain&quot;: true,<br>    &quot;domainColor&quot;: &quot;black&quot;,<br>    &quot;domainWidth&quot;: 3,<br>    &quot;tickSize&quot;: 5<br>  },<br>  &quot;axis&quot;: {<br>    &quot;ticks&quot;: false,<br>    &quot;tickCount&quot;: 4,<br>    &quot;tickBand&quot;: &quot;center&quot;,<br>    &quot;gridOpacity&quot;: 0.4,<br>    &quot;labelFontSize&quot;: 13,<br>    &quot;labelPadding&quot;: 8,<br>    &quot;labelFontWeight&quot;: &quot;bold&quot;,<br>    &quot;labelColor&quot;: &quot;grey&quot;<br>  },<br>  &quot;line&quot;: {<br>    &quot;interpolate&quot;: &quot;monotone&quot;,<br>    &quot;stroke&quot;: &quot;#569cd7&quot;,<br>    &quot;strokeWidth&quot;: 1.5<br>  },<br>  &quot;area&quot;: {<br>    &quot;interpolate&quot;: &quot;monotone&quot;,<br>    &quot;fill&quot;: &quot;#569cd7&quot;,<br>    &quot;opacity&quot;: 1,<br>    &quot;line&quot;: {<br>      &quot;stroke&quot;: &quot;white&quot;,<br>      &quot;strokeWidth&quot;: 1.5,<br>      &quot;yOffset&quot;: 1.5<br>    }<br>  },<br>  &quot;style&quot;: {<br>    &quot;_lineUN&quot;: {<br>      &quot;color&quot;: {&quot;expr&quot;: &quot;__colour_un&quot;}<br>    },<br>    &quot;_lineIIASA&quot;: {<br>      &quot;color&quot;: {<br>        &quot;expr&quot;: &quot;__colour_IIASA&quot;<br>      }<br>    },<br>    &quot;_lineIHME&quot;: {<br>      &quot;color&quot;: {&quot;expr&quot;: &quot;__colour_IHME&quot;}<br>    },<br>    &quot;_lineLower&quot;: {<br>      &quot;color&quot;: {<br>        &quot;expr&quot;: &quot;__colour_lower&quot;<br>      },<br>      &quot;strokeDash&quot;: [2, 3]<br>    },<br>    &quot;_lineHigher&quot;: {<br>      &quot;color&quot;: {<br>        &quot;expr&quot;: &quot;__colour_higher&quot;<br>      },<br>      &quot;strokeDash&quot;: [2, 3]<br>    }<br>  }<br>}</pre><p>And now when we style our marks in the Specification, it looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*-TamJ0fcth8MjOAcOcKJzA.png" /></figure><pre>{<br>      &quot;transform&quot;: [...],<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;line&quot;,<br>        &quot;style&quot;: &quot;_lineHigher&quot;    // &lt;&lt;--- reference the style named in our config<br>      },<br>      &quot;encoding&quot;: {<br>        &quot;y&quot;: {&quot;field&quot;: &quot;Higher&quot;}<br>      }<br>    }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F_sSOpLWZ85CYeewsSpsbg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*kkzVqTL0nNr8CQhdnkOUIg.png" /></figure><p>This is the main bulk of our chart build. The following steps are a little more complex, but you will see they are extremely effective. Onwards and forwards…!</p><h4>Step 6: Add Triangle Marks</h4><p>The digital publication on the right has several nice flourishes, that just make it pop, we will start adding those bit by bit. Starting with the triangles at the end of each line mark.</p><p>In order to achieve this we have to re-enter the layer-cake… we will place a layer INSIDE a layer… this is a summary what the new layer looks like — the key element to notice is the <em>encoding</em></p><pre>//--- NEW LAYER ---\\<br><br>{<br>      &quot;encoding&quot;: {<br>        &quot;x&quot;: {<br>          &quot;aggregate&quot;: &quot;max&quot;,  // get the max along the x-axis<br>          &quot;field&quot;: &quot;Date&quot;,<br>          &quot;type&quot;: &quot;temporal&quot;<br>        },<br>        &quot;y&quot;: {<br>          &quot;aggregate&quot;: {<br>            &quot;argmax&quot;: &quot;Date&quot;  // aggregate the y-axis and align to max of Date field (x-axis)<br>          },<br>          &quot;type&quot;: &quot;quantitative&quot;<br>        }<br>      },<br>//-- the above encoding is then applied to the following sub-layer<br>//-- each mark is bound to its measure which is assigned to the y-axis<br>      &quot;layer&quot;: [<br>        {<br>          &quot;mark&quot;: {  &quot;type&quot;: &quot;point&quot;, &quot;style&quot;: &quot;_endPointTriangle&quot; }<br>          &quot;encoding&quot;: { &quot;y&quot;: { &quot;field&quot;: &quot;UN&quot; } }<br>        },<br>        {<br>          &quot;mark&quot;: { &quot;type&quot;: &quot;point&quot;, &quot;style&quot;: &quot;_endPointTriangle&quot; },<br>          &quot;encoding&quot;: { &quot;y&quot;: {&quot;field&quot;: &quot;IIASA&quot;} }<br>        },<br>        {<br>          &quot;mark&quot;: { &quot;type&quot;: &quot;point&quot;, &quot;style&quot;: &quot;_endPointTriangle&quot; },<br>          &quot;encoding&quot;: { &quot;y&quot;: {&quot;field&quot;: &quot;IHME&quot;} }<br>        },<br>        {<br>          &quot;mark&quot;: { &quot;type&quot;: &quot;point&quot;, &quot;style&quot;: &quot;_endPointTriangle&quot; },<br>          &quot;encoding&quot;: { &quot;y&quot;: {&quot;field&quot;: &quot;Higher&quot;} }<br>        },<br>        {<br>          &quot;mark&quot;: { &quot;type&quot;: &quot;point&quot;, &quot;style&quot;: &quot;_endPointTriangle&quot; },<br>          &quot;encoding&quot;: { &quot;y&quot;: {&quot;field&quot;: &quot;Lower&quot;} }<br>        }<br>    }<br>  ]<br>}</pre><p>In broad context, the JSON is prepared as such:</p><pre>{     //-- start spec<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;encoding&quot;: {    //-- global encoding<br>    &quot;x&quot;: {...},<br>    &quot;y&quot;: {...}<br>  },<br>  &quot;layer&quot;: [       //-- start main layer object<br>    {...}, //-- mark spec<br>    {...}, //-- mark spec<br>    <br><br>    {      //-- start sub-layer spec<br>      &quot;encoding&quot;: {...}, //-- sub-layer encoding<br>      &quot;layer&quot;: [         //-- start sub-layer object<br>        {...},              //-- mark spec<br>        {...}               //-- mark spec<br>      ]                  //-- end sub-layer object<br>    }      //-- end sub-layer spec<br>  ]                //-- end main layer object<br>}    //-- end spec    </pre><p>In more detail, the JSON is structured as follows:</p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {...},<br>    &quot;y&quot;: {...}<br>  },<br>  &quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {...},<br>      &quot;encoding&quot;: { &quot;y&quot;: {&quot;field&quot;: &quot;UN_Historical&quot;} }<br>    },<br>    {<br>      &quot;mark&quot;: {...},<br>      &quot;encoding&quot;: { &quot;y&quot;: {&quot;field&quot;: &quot;IIASA&quot;} }<br>    },<br>    {<br>      &quot;mark&quot;: {...},<br>      &quot;encoding&quot;: { &quot;y&quot;: {&quot;field&quot;: &quot;IHME&quot;} }<br>    },<br>    {<br>      &quot;mark&quot;: {... },<br>      &quot;encoding&quot;: { &quot;y&quot;: {&quot;field&quot;: &quot;UN_Historical&quot;} }<br>    },<br>    {<br>      &quot;mark&quot;: {...},<br>      &quot;encoding&quot;: { &quot;y&quot;: {&quot;field&quot;: &quot;Higher&quot;} }<br>    },<br>    {<br>      &quot;mark&quot;: {...},<br>      &quot;encoding&quot;: {  &quot;y&quot;: {&quot;field&quot;: &quot;Lower&quot;} }<br>    },<br>    {<br>      &quot;mark&quot;: {...},<br>      &quot;encoding&quot;: { &quot;x&quot;: {}, &quot;y&quot;: {&quot;datum&quot;: 0} }<br>    },<br>//-- NEW SUB-LAYER STARTS HERE<br>    {<br>      &quot;encoding&quot;: {<br>        &quot;x&quot;: {<br>          &quot;aggregate&quot;: &quot;max&quot;,<br>          &quot;field&quot;: &quot;Date&quot;,<br>          &quot;type&quot;: &quot;temporal&quot;<br>        },<br>        &quot;y&quot;: {<br>          &quot;aggregate&quot;: {<br>            &quot;argmax&quot;: &quot;Date&quot;<br>          },<br>          &quot;type&quot;: &quot;quantitative&quot;<br>        }<br>      },<br>      &quot;layer&quot;: [<br>        {<br>          &quot;mark&quot;: {  &quot;type&quot;: &quot;point&quot;, &quot;style&quot;: &quot;_endPointTriangle&quot; }<br>          &quot;encoding&quot;: { &quot;y&quot;: { &quot;field&quot;: &quot;UN&quot; } }<br>        },<br>        {<br>          &quot;mark&quot;: { &quot;type&quot;: &quot;point&quot;, &quot;style&quot;: &quot;_endPointTriangle&quot; },<br>          &quot;encoding&quot;: { &quot;y&quot;: {&quot;field&quot;: &quot;IIASA&quot;} }<br>        },<br>        {<br>          &quot;mark&quot;: { &quot;type&quot;: &quot;point&quot;, &quot;style&quot;: &quot;_endPointTriangle&quot; },<br>          &quot;encoding&quot;: { &quot;y&quot;: {&quot;field&quot;: &quot;IHME&quot;} }<br>        },<br>        {<br>          &quot;mark&quot;: { &quot;type&quot;: &quot;point&quot;, &quot;style&quot;: &quot;_endPointTriangle&quot; },<br>          &quot;encoding&quot;: { &quot;y&quot;: {&quot;field&quot;: &quot;Higher&quot;} }<br>        },<br>        {<br>          &quot;mark&quot;: { &quot;type&quot;: &quot;point&quot;, &quot;style&quot;: &quot;_endPointTriangle&quot; },<br>          &quot;encoding&quot;: { &quot;y&quot;: {&quot;field&quot;: &quot;Lower&quot;} }<br>        }<br>    }<br>  ]<br>    }<br>  ]<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/773/1*lw5VDGJYh1o8hBN9S5khlQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R9tD91iW_9ZS-PVc6ArTAg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*kkzVqTL0nNr8CQhdnkOUIg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*Fc6nn6gmjBdKJ3FmkCbYDA.gif" /></figure><h4>Step 7: Add Text Marks</h4><p>We want to apply the same principle to our text marks, and because we’ve prepared the foundations (encoding) to our layer, the rest get’s easier from here. Let’s take this in chunks. But first, a bonus tip 🪄</p><p>We want to encode a text mark, and this text mark won’t contain a measure value, rather a manual input text string. One way to achieve this is to add a add a text attribute like this:</p><pre>{<br>  &quot;mark&quot;: {<br>    &quot;type&quot;: &quot;text&quot;,<br>    &quot;align&quot;: &quot;left&quot;,<br>    &quot;text&quot;:  &quot;UN EXPECTED SCENARIO&quot;        // line 1 <br>  },<br>  &quot;encoding&quot;: {           // encode on UN measure<br>    &quot;y&quot;: {&quot;field&quot;: &quot;UN&quot;}<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*6ppKOIPZf2ugFDpx7OljuQ.png" /></figure><p>We can also add multi-line text marks by placing the text strings in an array [square brackets]:</p><pre>{<br>  &quot;mark&quot;: {<br>    &quot;type&quot;: &quot;text&quot;,<br>    &quot;align&quot;: &quot;left&quot;,<br>    &quot;text&quot;:  [<br>              &quot;UN EXPECTED&quot;, // line 1<br>              &quot;SCENARIO&quot;     // line 2<br>             ]       <br>  },<br>  &quot;encoding&quot;: {           // encode on UN measure<br>    &quot;y&quot;: {&quot;field&quot;: &quot;UN&quot;}<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/438/1*p9GV_zpPFHcqJa0G8D9y0Q.png" /></figure><p>Pretty good, eh? Because we’ve already parameterised our different colour schemes for different marks, the formatting becomes much easier (just more copy and paste 🤓).</p><p>To create a little space from the triangle mark, we can increase the xOffset. The triangle marks had an xOffset = 10, we’ll just multiply this by 2.</p><pre>//--- text mark ---\\<br>{<br>  &quot;mark&quot;: {<br>    &quot;type&quot;: &quot;text&quot;,<br>    &quot;align&quot;: &quot;left&quot;,<br>    &quot;color&quot;: {<br>      &quot;expr&quot;: &quot;__colour_un&quot;<br>    },<br>    &quot;xOffset&quot;: {          // offset along x-axis<br>      &quot;expr&quot;: &quot;10 * 2&quot;    // 10 pixels multiplied by 2<br>    },<br>    &quot;text&quot;: [             // text string <br>      &quot;UN EXPECTED&quot;,        // line 1<br>      &quot;SCENARIO&quot;            // line 2<br>    ]<br>  },<br>  &quot;encoding&quot;: {           // encode on UN measure<br>    &quot;y&quot;: {&quot;field&quot;: &quot;UN&quot;}<br>  }<br>}</pre><p>Repeat for the remaining text marks, one text mark for each measure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/609/1*I__tyrZIACe-fwixu7YOAA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XUxALEqI_3iYanpo1_u4Pw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*kkzVqTL0nNr8CQhdnkOUIg.png" /></figure><h4>Step 8: Add Further Text Marks</h4><p>We can create the illusion of sub-texts, by additional text marks in the same fashion:</p><pre>{<br>          &quot;mark&quot;: {<br>            &quot;type&quot;: &quot;text&quot;,<br>            &quot;align&quot;: &quot;left&quot;,<br>            &quot;color&quot;: {&quot;expr&quot;: &quot;&#39;grey&#39;&quot;},<br>            &quot;xOffset&quot;: {<br>              &quot;expr&quot;: &quot;10 * 2&quot;<br>            },<br>            &quot;yOffset&quot;: 26,            //--- yOffset to place below main text<br>            &quot;text&quot;: [<br>              &quot;Fertility remains 0.5&quot;, <br>              &quot;children lower than in &quot;,<br>              &quot;the median scenario&quot;<br>            ]<br>          },<br>          &quot;encoding&quot;: {<br>            &quot;y&quot;: {&quot;field&quot;: &quot;Lower&quot;}    //--- [Lower] measure<br>          }<br>        }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/606/1*XNL2XczMFMtTzy4MXiLekg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PD2nI4_UWd5qp1rdTK8HXg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*kkzVqTL0nNr8CQhdnkOUIg.png" /></figure><h4>Step 9: Add Title and Subtitle</h4><p>For the Title and Subtitle, we can’t reproduce the font style exactly in the same we, but that doesn’t matter so much, our chart still looks awesome 🤗</p><pre>{<br>&quot;title&quot;: {<br>    &quot;font&quot;: &quot;arial&quot;,<br>    &quot;fontWeight&quot;: &quot;1000&quot;,                //-- bold<br>    &quot;text&quot;: [&quot;The World&quot;, &quot;in 2100&quot;],    //-- main title (2 lines)<br>    &quot;lineHeight&quot;: 45,<br>    &quot;fontStyle&quot;: &quot;italic&quot;,<br>    &quot;subtitle&quot;: [                        //-- subtitle (3 lines)<br>      &quot;Although the UN&#39;s projections are&quot;,<br>      &quot; the most widely used, alternative models &quot;,<br>      &quot;are predicting both a lower and earlier peak.&quot;<br>    ],<br>    &quot;fontSize&quot;: 50,<br>    &quot;subtitleFontSize&quot;: 15,<br>    &quot;subtitleColor&quot;: &quot;dimgrey&quot;,<br>    &quot;zindex&quot;: 1,<br>    &quot;anchor&quot;: &quot;start&quot;,                  // start (left aligned)<br>    &quot;offset&quot;: -150                      //-- offset to adjust the position<br>  },<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},    //--- dataset starts here<br>  &quot;encoding&quot;: {                   //--- global encoding<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;Date&quot;,<br>      &quot;type&quot;: &quot;temporal&quot;<br>    },<br>    &quot;y&quot;: {<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;aggregate&quot;: &quot;sum&quot;<br>    }<br>  }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y2fs6d9AgeuiVCDyZ2No-Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*kkzVqTL0nNr8CQhdnkOUIg.png" /></figure><h4>Step 10: Add Rule Mark (getting closer)</h4><p>Now we’ll add a dotted line at the 2022 year mark to separate the historic figures with projected figures.</p><pre>{<br>  &quot;transform&quot;: [<br>    {<br>      &quot;filter&quot;: &quot;datum.Year == 2022&quot;<br>    }<br>  ],<br>  &quot;mark&quot;: {<br>    &quot;type&quot;: &quot;rule&quot;,<br>    &quot;color&quot;: &quot;black&quot;,<br>    &quot;strokeWidth&quot;: &quot;1&quot;,<br>    &quot;strokeDash&quot;: [2, 2],<br>    &quot;xOffset&quot;: 0.5<br>  },<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {&quot;field&quot;: &quot;Date&quot;},<br>    &quot;y&quot;: {&quot;datum&quot;: 10000000000}    // place the mark around the 100 million value<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*igBSr5dYkJyHAG_5drw-Eg.png" /></figure><h4>Step 11: Add SVG markers and Legend Image</h4><p>At this juncture, we will be adding a wonderful feature by using SVG icons… I put my hand up here and admit, I genuinely have difficulty creating SVG path-icons that sit nicely at the appropriate data point. I offer sincere thanks to Deneb creator <a href="https://medium.com/u/dcfbf31b00d6">Daniel Marsh-Patrick</a> for creating the SVG icon in exactly the form I desired… pure perfection. You can use any SVG creating tool, such as InkScape.</p><p>There’s some Vega-Lite documentation on <a href="https://vega.github.io/vega-lite/docs/point.html#properties">Point mark</a>s, which kind of makes sense, and kind of doesn’t 😅</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LefJ38OITZi3r-7BH8W6VA.png" /></figure><p>Again, we will follow an efficient coding practice, so we will add the SVG icon within our style{} property. The shape is defined by the SVG path.</p><pre>//--- config ---\\\<br>&quot;svgIcon&quot;: {<br>      &quot;shape&quot;: &quot;m -0.97916486,0.97980576 4.8839e-4,-0.41747069 c 4.2682e-4,-0.36480847 0.39320648,-0.42104751 0.62106371,-0.42104751 l 0.68356092,1.25e-6 c 0.22785724,0 0.64450262,0.0687798 0.64449998,0.41695964 l -3.02e-6,0.42104746 z m 1.52352285,-1.38398524 c 0,0.2988249 -0.24224552,0.54107033 -0.54107037,0.5410703 -0.29882484,0 -0.54107037,-0.24224543 -0.54107037,-0.5410703 0,-0.2988249 0.24224553,-0.54107034 0.54107037,-0.54107034 0.29882485,0 0.54107037,0.24224542 0.54107037,0.54107034 z&quot;,<br>      &quot;size&quot;: 150,<br>      &quot;filled&quot;: true,<br>      &quot;stroke&quot;: &quot;#faf7e6&quot;,<br>      &quot;strokeWidth&quot;: 1,<br>      &quot;opacity&quot;: 1<br>    }</pre><pre>//--- spec ---\\<br>{<br>  &quot;transform&quot;: [<br>    {<br>      &quot;filter&quot;: &quot;datum.Year == 2064&quot;<br>    }<br>  ],<br>  &quot;mark&quot;: {<br>    &quot;type&quot;: &quot;point&quot;,<br>    &quot;style&quot;: &quot;svgIcon&quot;,          // &lt;-- style placeholder for our svg icons<br>    &quot;color&quot;: {<br>      &quot;expr&quot;: &quot;__colour_IHME&quot;<br>    }<br>  },<br>  &quot;encoding&quot;: {<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;IHME&quot;,<br>      &quot;type&quot;: &quot;quantitative&quot;<br>    }<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vfQqLFVBpWfpDc9Iq3-mcw.png" /></figure><h4>Step 12: Finishing Touches</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2I2NWO6OVp7jIOHhn9qYjg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/811/1*rKkkHnQRmsK3KM72jwiJ6Q.png" /></figure><pre>{<br>  &quot;title&quot;: {<br>    &quot;font&quot;: &quot;impact&quot;,<br>    &quot;fontWeight&quot;: &quot;normal&quot;,<br>    &quot;text&quot;: [<br>      &quot;THE WORLD&quot;,<br>      &quot;IN 2100&quot;<br>    ],<br>    &quot;lineHeight&quot;: 45,<br>    &quot;fontStyle&quot;: &quot;italic&quot;,<br>    &quot;subtitle&quot;: [<br>      &quot;PREDICTING GLOBAL POPULATION&quot;,<br>      &quot; ​&quot;,<br>      &quot;Although the UN&#39;s projections are the most&quot;,<br>      &quot;widely used, alternative models are predicting&quot;,<br>      &quot;both a lower and earlier peak.&quot;<br>    ],<br>    &quot;fontSize&quot;: 50,<br>    &quot;subtitleFontSize&quot;: 12,<br>    &quot;subtitleColor&quot;: &quot;dimgrey&quot;,<br>    &quot;zindex&quot;: 1,<br>    &quot;anchor&quot;: &quot;start&quot;,<br>    &quot;offset&quot;: -200<br>  },<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;spacing&quot;: 10,<br>  &quot;vconcat&quot;: [<br>    {<br>      &quot;height&quot;: 600,<br>      &quot;width&quot;: 350,<br>      &quot;encoding&quot;: {<br>        &quot;x&quot;: {<br>          &quot;field&quot;: &quot;Date&quot;,<br>          &quot;type&quot;: &quot;temporal&quot;<br>        },<br>        &quot;y&quot;: {<br>          &quot;type&quot;: &quot;quantitative&quot;,<br>          &quot;aggregate&quot;: &quot;sum&quot;<br>        }<br>      },<br>      &quot;layer&quot;: [<br>        {<br>          &quot;transform&quot;: [<br>            {<br>              &quot;filter&quot;: &quot;datum.Year &lt;= 2022&quot;<br>            }<br>          ],<br>          &quot;mark&quot;: {<br>            &quot;type&quot;: &quot;area&quot;,<br>            &quot;opacity&quot;: 1<br>          },<br>          &quot;encoding&quot;: {<br>            &quot;y&quot;: {<br>              &quot;field&quot;: &quot;UN_Historical&quot;<br>            }<br>          }<br>        },<br>        {<br>          &quot;transform&quot;: [<br>            {<br>              &quot;filter&quot;: &quot;isValid(datum.IIASA)&quot;<br>            }<br>          ],<br>          &quot;mark&quot;: {<br>            &quot;type&quot;: &quot;line&quot;,<br>            &quot;style&quot;: &quot;_lineIIASA&quot;<br>          },<br>          &quot;encoding&quot;: {<br>            &quot;y&quot;: {&quot;field&quot;: &quot;IIASA&quot;}<br>          }<br>        },<br>        {<br>          &quot;transform&quot;: [<br>            {<br>              &quot;filter&quot;: &quot;isValid(datum.IHME)&quot;<br>            }<br>          ],<br>          &quot;mark&quot;: {<br>            &quot;type&quot;: &quot;line&quot;,<br>            &quot;style&quot;: &quot;_lineIHME&quot;<br>          },<br>          &quot;encoding&quot;: {<br>            &quot;y&quot;: {&quot;field&quot;: &quot;IHME&quot;}<br>          }<br>        },<br>        {<br>          &quot;mark&quot;: {<br>            &quot;type&quot;: &quot;line&quot;,<br>            &quot;style&quot;: &quot;_lineUN&quot;<br>          },<br>          &quot;encoding&quot;: {<br>            &quot;y&quot;: {<br>              &quot;field&quot;: &quot;UN_Historical&quot;<br>            }<br>          }<br>        },<br>        {<br>          &quot;transform&quot;: [<br>            {<br>              &quot;filter&quot;: &quot;isValid(datum.Higher)&quot;<br>            }<br>          ],<br>          &quot;mark&quot;: {<br>            &quot;type&quot;: &quot;line&quot;,<br>            &quot;style&quot;: &quot;_lineHigher&quot;<br>          },<br>          &quot;encoding&quot;: {<br>            &quot;y&quot;: {&quot;field&quot;: &quot;Higher&quot;}<br>          }<br>        },<br>        {<br>          &quot;transform&quot;: [<br>            {<br>              &quot;filter&quot;: &quot;isValid(datum.Lower)&quot;<br>            }<br>          ],<br>          &quot;mark&quot;: {<br>            &quot;type&quot;: &quot;line&quot;,<br>            &quot;style&quot;: &quot;_lineLower&quot;<br>          },<br>          &quot;encoding&quot;: {<br>            &quot;y&quot;: {&quot;field&quot;: &quot;Lower&quot;}<br>          }<br>        },<br>        {<br>          &quot;encoding&quot;: {<br>            &quot;x&quot;: {<br>              &quot;aggregate&quot;: &quot;max&quot;,<br>              &quot;field&quot;: &quot;Date&quot;,<br>              &quot;type&quot;: &quot;temporal&quot;<br>            },<br>            &quot;y&quot;: {<br>              &quot;aggregate&quot;: {<br>                &quot;argmax&quot;: &quot;Date&quot;<br>              },<br>              &quot;type&quot;: &quot;quantitative&quot;<br>            }<br>          },<br>          &quot;layer&quot;: [<br>            {<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;point&quot;,<br>                &quot;style&quot;: &quot;_endPointTriangle&quot;,<br>                &quot;xOffset&quot;: 10,<br>                &quot;color&quot;: {<br>                  &quot;expr&quot;: &quot;__colour_un&quot;<br>                }<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;y&quot;: {<br>                  &quot;field&quot;: &quot;UN&quot;,<br>                  &quot;type&quot;: &quot;quantitative&quot;<br>                }<br>              }<br>            },<br>            {<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;point&quot;,<br>                &quot;style&quot;: &quot;_endPointTriangle&quot;,<br>                &quot;xOffset&quot;: 10,<br>                &quot;color&quot;: {<br>                  &quot;expr&quot;: &quot;__colour_IIASA&quot;<br>                }<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;y&quot;: {&quot;field&quot;: &quot;IIASA&quot;}<br>              }<br>            },<br>            {<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;point&quot;,<br>                &quot;style&quot;: &quot;_endPointTriangle&quot;,<br>                &quot;xOffset&quot;: 10,<br>                &quot;color&quot;: {<br>                  &quot;expr&quot;: &quot;__colour_IHME&quot;<br>                }<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;y&quot;: {&quot;field&quot;: &quot;IHME&quot;}<br>              }<br>            },<br>            {<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;point&quot;,<br>                &quot;style&quot;: &quot;_arrowUp&quot;,<br>                &quot;xOffset&quot;: 10,<br>                &quot;color&quot;: {<br>                  &quot;expr&quot;: &quot;__colour_higher&quot;<br>                }<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;y&quot;: {&quot;field&quot;: &quot;Higher&quot;}<br>              }<br>            },<br>            {<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;point&quot;,<br>                &quot;style&quot;: &quot;_arrowDown&quot;,<br>                &quot;xOffset&quot;: 10,<br>                &quot;color&quot;: {<br>                  &quot;expr&quot;: &quot;__colour_lower&quot;<br>                }<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;y&quot;: {&quot;field&quot;: &quot;Lower&quot;}<br>              }<br>            },<br>            {<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;text&quot;,<br>                &quot;align&quot;: &quot;left&quot;,<br>                &quot;fontWeight&quot;: &quot;bold&quot;,<br>                &quot;color&quot;: {<br>                  &quot;expr&quot;: &quot;__colour_un&quot;<br>                },<br>                &quot;xOffset&quot;: {<br>                  &quot;expr&quot;: &quot;10 * 2&quot;<br>                },<br>                &quot;text&quot;: [<br>                  &quot;UN PROBABLISTIC&quot;,<br>                  &quot;SCENARIO&quot;<br>                ]<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;y&quot;: {&quot;field&quot;: &quot;UN&quot;}<br>              }<br>            },<br>            {<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;text&quot;,<br>                &quot;align&quot;: &quot;left&quot;,<br>                &quot;fontWeight&quot;: &quot;bold&quot;,<br>                &quot;baseline&quot;: &quot;middle&quot;,<br>                &quot;color&quot;: {<br>                  &quot;expr&quot;: &quot;__colour_IIASA&quot;<br>                },<br>                &quot;xOffset&quot;: {<br>                  &quot;expr&quot;: &quot;10 * 2&quot;<br>                },<br>                &quot;yOffset&quot;: -2,<br>                &quot;text&quot;: [&quot;IIASA*&quot;]<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;y&quot;: {&quot;field&quot;: &quot;IIASA&quot;}<br>              }<br>            },<br>            {<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;text&quot;,<br>                &quot;align&quot;: &quot;left&quot;,<br>                &quot;fontWeight&quot;: &quot;bold&quot;,<br>                &quot;baseline&quot;: &quot;middle&quot;,<br>                &quot;color&quot;: {<br>                  &quot;expr&quot;: &quot;__colour_IHME&quot;<br>                },<br>                &quot;xOffset&quot;: {<br>                  &quot;expr&quot;: &quot;10 * 2&quot;<br>                },<br>                &quot;yOffset&quot;: 2,<br>                &quot;text&quot;: [&quot;IHME**&quot;]<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;y&quot;: {&quot;field&quot;: &quot;IHME&quot;}<br>              }<br>            },<br>            {<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;text&quot;,<br>                &quot;align&quot;: &quot;left&quot;,<br>                &quot;fontWeight&quot;: &quot;bold&quot;,<br>                &quot;color&quot;: {<br>                  &quot;expr&quot;: &quot;__colour_higher&quot;<br>                },<br>                &quot;xOffset&quot;: {<br>                  &quot;expr&quot;: &quot;10 * 2&quot;<br>                },<br>                &quot;text&quot;: [<br>                  &quot;UN HIGH FERTILITY&quot;,<br>                  &quot;SCENARIO&quot;<br>                ]<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;y&quot;: {&quot;field&quot;: &quot;Higher&quot;}<br>              }<br>            },<br>            {<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;text&quot;,<br>                &quot;align&quot;: &quot;left&quot;,<br>                &quot;color&quot;: {<br>                  &quot;expr&quot;: &quot;&#39;grey&#39;&quot;<br>                },<br>                &quot;xOffset&quot;: {<br>                  &quot;expr&quot;: &quot;10 * 2&quot;<br>                },<br>                &quot;yOffset&quot;: 26,<br>                &quot;text&quot;: [<br>                  &quot;Fertility remains 0.5&quot;,<br>                  &quot;children higher than in &quot;,<br>                  &quot;the median scenario&quot;<br>                ]<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;y&quot;: {&quot;field&quot;: &quot;Higher&quot;}<br>              }<br>            },<br>            {<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;text&quot;,<br>                &quot;align&quot;: &quot;left&quot;,<br>                &quot;fontWeight&quot;: &quot;bold&quot;,<br>                &quot;color&quot;: {<br>                  &quot;expr&quot;: &quot;__colour_lower&quot;<br>                },<br>                &quot;xOffset&quot;: {<br>                  &quot;expr&quot;: &quot;10 * 2&quot;<br>                },<br>                &quot;text&quot;: [<br>                  &quot;UN LOW FERTILITY&quot;,<br>                  &quot;SCENARIO&quot;<br>                ]<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;y&quot;: {&quot;field&quot;: &quot;Lower&quot;}<br>              }<br>            },<br>            {<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;text&quot;,<br>                &quot;align&quot;: &quot;left&quot;,<br>                &quot;color&quot;: {<br>                  &quot;expr&quot;: &quot;&#39;grey&#39;&quot;<br>                },<br>                &quot;xOffset&quot;: {<br>                  &quot;expr&quot;: &quot;10 * 2&quot;<br>                },<br>                &quot;yOffset&quot;: 26,<br>                &quot;text&quot;: [<br>                  &quot;Fertility remains 0.5&quot;,<br>                  &quot;children lower than in &quot;,<br>                  &quot;the median scenario&quot;<br>                ]<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;y&quot;: {&quot;field&quot;: &quot;Lower&quot;}<br>              }<br>            },<br>            {<br>              &quot;transform&quot;: [<br>                {<br>                  &quot;filter&quot;: &quot;datum.Year == 2022&quot;<br>                }<br>              ],<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;rule&quot;,<br>                &quot;color&quot;: &quot;black&quot;,<br>                &quot;strokeWidth&quot;: &quot;1&quot;,<br>                &quot;strokeDash&quot;: [2, 2],<br>                &quot;xOffset&quot;: 0.5<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;x&quot;: {&quot;field&quot;: &quot;Date&quot;},<br>                &quot;y&quot;: {<br>                  &quot;datum&quot;: 10000000000<br>                }<br>              }<br>            },<br>            {<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;rule&quot;,<br>                &quot;color&quot;: {<br>                  &quot;expr&quot;: &quot;__background_colour&quot;<br>                },<br>                &quot;strokeWidth&quot;: 0.7,<br>                &quot;yOffset&quot;: -0.6<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;x&quot;: {},<br>                &quot;y&quot;: {&quot;datum&quot;: 0}<br>              }<br>            },<br>            {<br>              &quot;transform&quot;: [<br>                {<br>                  &quot;filter&quot;: &quot;datum.Year == 2022  &quot;<br>                }<br>              ],<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;text&quot;,<br>                &quot;fontWeight&quot;: &quot;bolder&quot;,<br>                &quot;fontStyle&quot;: &quot;italic&quot;,<br>                &quot;align&quot;: &quot;left&quot;,<br>                &quot;baseline&quot;: &quot;top&quot;,<br>                &quot;filled&quot;: true,<br>                &quot;size&quot;: 12,<br>                &quot;color&quot;: &quot;black&quot;,<br>                &quot;xOffset&quot;: 1,<br>                &quot;yOffset&quot;: -40,<br>                &quot;text&quot;: [&quot;▸ Projected&quot;]<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;x&quot;: {&quot;field&quot;: &quot;Date&quot;},<br>                &quot;y&quot;: {&quot;datum&quot;: 0}<br>              }<br>            },<br>            {<br>              &quot;transform&quot;: [<br>                {<br>                  &quot;filter&quot;: &quot;datum.Year == 2022  &quot;<br>                }<br>              ],<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;text&quot;,<br>                &quot;fontWeight&quot;: &quot;bolder&quot;,<br>                &quot;fontStyle&quot;: &quot;italic&quot;,<br>                &quot;align&quot;: &quot;right&quot;,<br>                &quot;baseline&quot;: &quot;top&quot;,<br>                &quot;filled&quot;: true,<br>                &quot;size&quot;: 12,<br>                &quot;color&quot;: &quot;white&quot;,<br>                &quot;xOffset&quot;: -5,<br>                &quot;yOffset&quot;: -40,<br>                &quot;text&quot;: [<br>                  &quot;UN Historical ◂&quot;<br>                ]<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;x&quot;: {&quot;field&quot;: &quot;Date&quot;},<br>                &quot;y&quot;: {&quot;datum&quot;: 0}<br>              }<br>            },<br>            {<br>              &quot;transform&quot;: [<br>                {<br>                  &quot;filter&quot;: &quot;datum.Year == 2064&quot;<br>                }<br>              ],<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;point&quot;,<br>                &quot;style&quot;: &quot;svgIcon&quot;,<br>                &quot;color&quot;: {<br>                  &quot;expr&quot;: &quot;__colour_IHME&quot;<br>                }<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;y&quot;: {<br>                  &quot;field&quot;: &quot;IHME&quot;,<br>                  &quot;type&quot;: &quot;quantitative&quot;<br>                }<br>              }<br>            },<br>            {<br>              &quot;transform&quot;: [<br>                {<br>                  &quot;filter&quot;: &quot;datum.Year == 2070&quot;<br>                }<br>              ],<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;point&quot;,<br>                &quot;style&quot;: &quot;svgIcon&quot;,<br>                &quot;color&quot;: {<br>                  &quot;expr&quot;: &quot;__colour_IIASA&quot;<br>                }<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;y&quot;: {<br>                  &quot;field&quot;: &quot;IIASA&quot;,<br>                  &quot;type&quot;: &quot;quantitative&quot;<br>                }<br>              }<br>            },<br>            {<br>              &quot;transform&quot;: [<br>                {<br>                  &quot;filter&quot;: &quot;datum.Year == 2086&quot;<br>                }<br>              ],<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;point&quot;,<br>                &quot;style&quot;: &quot;svgIcon&quot;,<br>                &quot;color&quot;: {<br>                  &quot;expr&quot;: &quot;__colour_un&quot;<br>                }<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;y&quot;: {<br>                  &quot;field&quot;: &quot;UN&quot;,<br>                  &quot;type&quot;: &quot;quantitative&quot;<br>                }<br>              }<br>            },<br>            {<br>              &quot;transform&quot;: [<br>                {<br>                  &quot;filter&quot;: &quot;datum.Year == 2050&quot;<br>                }<br>              ],<br>              &quot;mark&quot;: {<br>                &quot;type&quot;: &quot;text&quot;,<br>                &quot;color&quot;: &quot;grey&quot;,<br>                &quot;fontWeight&quot;: &quot;bold&quot;,<br>                &quot;xOffset&quot;: -10<br>              },<br>              &quot;encoding&quot;: {<br>                &quot;text&quot;: {<br>                  &quot;datum&quot;: &quot;15B&quot;<br>                },<br>                &quot;x&quot;: {&quot;field&quot;: &quot;Date&quot;},<br>                &quot;y&quot;: {<br>                  &quot;datum&quot;: 15000000000<br>                }<br>              }<br>            }<br>          ]<br>        }<br>      ]<br>    },<br>    {<br>      &quot;data&quot;: {&quot;values&quot;: [{}]},<br>      &quot;layer&quot;: [<br>        {<br>          &quot;mark&quot;: {<br>            &quot;type&quot;: &quot;text&quot;,<br>            &quot;dx&quot;: -30,<br>            &quot;align&quot;: &quot;left&quot;,<br>            &quot;font&quot;: &quot;helvetica&quot;,<br>            &quot;fontSize&quot;: &quot;7&quot;,<br>            &quot;fontWeight&quot;: &quot;bold&quot;,<br>            &quot;color&quot;: &quot;#404040&quot;<br>          },<br>          &quot;encoding&quot;: {<br>            &quot;text&quot;: {<br>              &quot;value&quot;: [<br>                &quot;UN Projections for 2022. IHME projections as of 2020. IIASA projections as of 2014&quot;,<br>                &quot;Source: UN, IHME, IIASA&quot;<br>              ]<br>            }<br>          }<br>        },<br>        {<br>          &quot;mark&quot;: {<br>            &quot;type&quot;: &quot;text&quot;,<br>            &quot;dx&quot;: 280,<br>            &quot;align&quot;: &quot;left&quot;,<br>            &quot;fontWeight&quot;: &quot;normal&quot;,<br>            &quot;font&quot;: &quot;helvetica&quot;,<br>            &quot;fontSize&quot;: &quot;7&quot;,<br>            &quot;color&quot;: {<br>              &quot;expr&quot;: &quot;__colour_IIASA&quot;<br>            }<br>          },<br>          &quot;encoding&quot;: {<br>            &quot;text&quot;: {<br>              &quot;value&quot;: [<br>                &quot;*International Institute for Applied Systems Analysis&quot;<br>              ]<br>            }<br>          }<br>        },<br>        {<br>          &quot;mark&quot;: {<br>            &quot;type&quot;: &quot;text&quot;,<br>            &quot;dx&quot;: 280,<br>            &quot;dy&quot;: 10,<br>            &quot;align&quot;: &quot;left&quot;,<br>            &quot;fontWeight&quot;: &quot;normal&quot;,<br>            &quot;font&quot;: &quot;helvetica&quot;,<br>            &quot;fontSize&quot;: &quot;7&quot;,<br>            &quot;color&quot;: {<br>              &quot;expr&quot;: &quot;__colour_IHME&quot;<br>            }<br>          },<br>          &quot;encoding&quot;: {<br>            &quot;text&quot;: {<br>              &quot;value&quot;: [<br>                &quot;**Institute for Health Metrics and Evaluation&quot;<br>              ]<br>            }<br>          }<br>        }<br>      ]<br>    }<br>  ]<br>}</pre><p>This was a big project, I’ve covered each step in more detail in the .PBIX below. I hope this has been a useful and enjoyable exercise… until next time!</p><p>#StayQueryous</p><p>🔗<strong>Github link to PBIX: </strong><a href="https://github.com/PBIQueryous/Deneb/blob/main/Medium-VegaLite-Series/EP09%20Deneb%20VegaLite%20Series%20-%20World%20Population%20In%202100.pbix"><strong>EP09 — UN Population Projection.pbix</strong></a><strong><br>🔍Soure: </strong><a href="https://www.visualcapitalist.com/when-will-the-global-population-reach-its-peak/"><strong>When will the Global Population Reach Its Peak</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*URMQxNOaWlLwolWdwTjv9Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2mEoX1NDQPDbHwb6o-I6-Q.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74baa48f716c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deneb & Vega-Lite Walkthrough Series | EP08: Small Multiples (Facets)]]></title>
            <link>https://pbi-datavizzle.medium.com/deneb-vega-lite-walkthrough-series-ep08-small-multiples-facets-0e8d5f7f42b1?source=rss-23e0d5d396ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/0e8d5f7f42b1</guid>
            <category><![CDATA[vega-lite]]></category>
            <category><![CDATA[power-bi]]></category>
            <category><![CDATA[deneb]]></category>
            <category><![CDATA[power-bi-tutorials]]></category>
            <category><![CDATA[data-visualization]]></category>
            <dc:creator><![CDATA[PBI DataVizzle]]></dc:creator>
            <pubDate>Tue, 30 Apr 2024 11:02:27 GMT</pubDate>
            <atom:updated>2024-04-30T11:31:12.196Z</atom:updated>
            <content:encoded><![CDATA[<h4>For all the movie-lovers out there, we shall utilise facets to better identify which trilogy movie is best based on IMDB ratings. 🕊️ 🧙🏼‍♂️ ✨</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0akl_1-02ietbh-naruaNA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*np4uF0xj5OyC7un0SKTCMg.png" /></figure><p>💌 <em>PBIX file available at the end of the article… Enjoy!</em></p><h3>Recap</h3><ul><li><a href="https://medium.com/@pbiqueryous/power-bi-deneb-vega-lite-series-ep01-marks-and-encoding-lets-learn-to-run-before-we-fly-87f4961a2fa5">EP1</a> — Marks and Encoding</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-series-ep02-mark-types-cd7a99d4a23c">EP2</a> — Mark Types</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walk-through-series-ep03-styling-mark-properties-4dddb9e0f353">EP3</a> — Styling Mark Propreties (Part 1)</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep04-styling-mark-properties-pt2-6cafb8a2e552">EP4</a> — Styling Mark Properties (Part 2)</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep05-layers-4ab67067f717">EP5</a> — Layers (Multiple Views)</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep06-expressions-conditional-formatting-pt1-eab3ca34f0de">EP6</a> — Expressions &amp; Conditional Formatting</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep07-number-formats-5ef46090d765">EP7 </a>— Number Formatting</li></ul><h3>The Holy Trilogy</h3><blockquote>…he who finds the Holy Grail must face three challenges…</blockquote><h4>Power Query and the Last Crusade</h4><p>The data in this instances was compiled manually, using a combination of IMDB web-scraping, and manual input… alas, Excel is forever your friend (and don’t you forget it! 🤓😏). After building my dataset in Excel, I created a manual input table in Power Query and pasted the values in there. Feel free to compile and improve the underlying data with more film entries!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/1*HKOwUlhOoA1wcVE9Ok9lsA.png" /><figcaption>Data layout</figcaption></figure><p>We only require three fields:</p><ul><li>trilogy</li><li>film</li><li>imdb_score</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/189/1*wfNJzobvjUjmyyxXOtIjQA.png" /></figure><h4>Vega-Lite and the Temple of Doom</h4><p>We will go through a step-by-step process, making mistakes along the way, and learning from these challenges in real-time 🪄🥹 — this is a safe place for personal growth and development 🤗🫂</p><p><strong>Starting Point:</strong></p><pre>{<br>  &quot;$schema&quot;: &quot;https://vega.github.io/schema/vega-lite/v5.json&quot;,<br>  &quot;description&quot;: &quot;A faceted chart showing movie ratings across different franchises.&quot;,<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;height&quot;: 80,<br>  &quot;width&quot;: 95,<br>  &quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,            // bar mark<br>        &quot;stroke&quot;: &quot;#444&quot;<br>      },<br>      &quot;encoding&quot;: {<br>        &quot;x&quot;: {<br>          &quot;field&quot;: &quot;film&quot;,        // x-axis<br>          &quot;type&quot;: &quot;ordinal&quot;,      // numerical order<br>          &quot;title&quot;: null,<br>          &quot;axis&quot;: {<br>            &quot;labels&quot;: true,<br>            &quot;labelAngle&quot;: 0,<br>            &quot;ticks&quot;: false<br>          }<br>        },<br>        &quot;y&quot;: {<br>          &quot;field&quot;: &quot;imdb_score&quot;,  // y-axis<br>          &quot;type&quot;: &quot;quantitative&quot;, // quantitative (aggregatable)<br>          &quot;aggregate&quot;: &quot;mean&quot;,    // aggregate the average (for now)<br>          &quot;title&quot;: null,<br>          &quot;axis&quot;: {<br>            &quot;labels&quot;: true,<br>            &quot;format&quot;: &quot;.0%&quot;,      // percent number format, 1 decimal (%)<br>            &quot;ticks&quot;: false,<br>            &quot;grid&quot;: false<br>          }<br>        }<br>      }<br>    }<br>  ]<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*IRuycsHD6-LYLMaCpnv1JQ.png" /><figcaption>Starting Point</figcaption></figure><p><strong>Identifying Aesthetic Gripes</strong></p><p>The first <a href="https://www.merriam-webster.com/dictionary/niggle#:~:text=2%20of%202-,noun,trifling%20doubt%2C%20objection%2C%20or%20complaint">niggle</a> we need to address is the y-axis. We know we are working with percentage, so our range is 0% to 100% (0 to 1). By default, Deneb and PowerBI will always summarise the data values according to their min/max value range. We can explicitly set this range, very much in the same way you set the number range in PowerBI’s Y-axis options:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/183/1*uMB_8GFDMH1kxcT2kEY7oQ.png" /></figure><p>In Vega-Lite, we use the scale expression to modify the domain (range) of values:</p><pre>&quot;scale&quot;: {&quot;domain&quot;: [0, 1]}</pre><p>And in context:</p><pre>{<br>  &quot;$schema&quot;: &quot;https://vega.github.io/schema/vega-lite/v5.json&quot;,<br>  &quot;description&quot;: &quot;A faceted chart showing movie ratings across different franchises.&quot;,<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;height&quot;: 80,<br>  &quot;width&quot;: 95,<br>  &quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {...},<br>      &quot;encoding&quot;: {<br>        &quot;x&quot;: {...},<br>        &quot;y&quot;: {<br>          &quot;field&quot;: &quot;imdb_score&quot;,<br>          &quot;type&quot;: &quot;quantitative&quot;,<br>          &quot;aggregate&quot;: &quot;mean&quot;,<br>          &quot;title&quot;: null,<br>          &quot;scale&quot;: {&quot;domain&quot;: [0, 1]},  // &lt;-- set the y-axis value range<br>          &quot;axis&quot;: {...}<br>        }<br>      }<br>    }<br>  ]<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/619/1*PuaY9PAv8ah4_kjMT6wGPw.png" /><figcaption>Chart with y-axis min/max range defined (0, 1)</figcaption></figure><p><strong>Separate chart by trilogy category</strong></p><p>So, to create small multiples, we can use our existing knowledge and add a legend, binding our trilogy field to the color channel. Let’s give it a try:</p><pre>{<br>  &quot;$schema&quot;: &quot;https://vega.github.io/schema/vega-lite/v5.json&quot;,<br>  &quot;description&quot;: &quot;A faceted chart showing movie ratings across different franchises.&quot;,<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;height&quot;: 80,<br>  &quot;width&quot;: 95,<br>  &quot;transform&quot;: [<br>    <br>  ],<br>  &quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;stroke&quot;: &quot;#444&quot;,<br>        &quot;strokeWidth&quot;: 0.5,<br>        &quot;strokeOpacity&quot;: 0.2<br>      },<br>      &quot;encoding&quot;: {<br>        &quot;x&quot;: {<br>          &quot;field&quot;: &quot;film&quot;,<br>          &quot;type&quot;: &quot;ordinal&quot;,<br>          &quot;title&quot;: null,<br>          &quot;axis&quot;: {<br>            &quot;labels&quot;: true,<br>            &quot;labelAngle&quot;: 0,<br>            &quot;ticks&quot;: false<br>          }<br>        },<br>        &quot;y&quot;: {<br>          &quot;field&quot;: &quot;imdb_score&quot;,<br>          &quot;type&quot;: &quot;quantitative&quot;,<br>          &quot;aggregate&quot;: &quot;mean&quot;,<br>          &quot;title&quot;: null,<br>          &quot;scale&quot;: {&quot;domain&quot;: [0, 1]},<br>          &quot;axis&quot;: {<br>            &quot;labels&quot;: true,<br>            &quot;format&quot;: &quot;.0%&quot;,<br>            &quot;ticks&quot;: false,<br>            &quot;grid&quot;: false<br>          }<br>        },<br>        &quot;color&quot;: {<br>          &quot;field&quot;: &quot;trilogy&quot;            // &lt;--- distribute by trilogy values<br>        }<br>      }<br>    }<br>  ]<br>}</pre><p>….launch it…. 🙏🏼</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/342/1*ffoFPBlMqTU7uiwjw4HWmQ.png" /></figure><p>Hmmn, not quite what we had in mind. Instead of adding a new chart for each category, it has stacked the categories and changed only the colour 💡. We need a slightly different approach, but we certainly are on the right tracks… let’s break it down in to smaller parts so we can make better sense of it. We will filter down the trilogy values.</p><p>Add a filter transform:<strong><br></strong>We’ll add this transform at the top of our code to apply to the entire layer.</p><pre>&quot;transform&quot;: [<br>    {<br>      &quot;filter&quot;: &quot;datum.trilogy == &#39;Alien&#39; || datum.trilogy == &#39;Jaws&#39; || datum.trilogy == &#39;Mighty Ducks&#39; &quot;<br>    }<br>  ],</pre><p>And in context:</p><pre>{<br>  &quot;$schema&quot;: &quot;https://vega.github.io/schema/vega-lite/v5.json&quot;,<br>  &quot;description&quot;: &quot;A faceted chart showing movie ratings across different franchises.&quot;,<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;height&quot;: 80,<br>  &quot;width&quot;: 95,<br><br>// apply a transform to the layer<br>// choose 3 films where trilogy = &quot;Alien&quot;, &quot;Jaws&quot; or &quot;Mighty Ducks&quot;<br>  &quot;transform&quot;: [<br>    {<br>      &quot;filter&quot;: &quot;datum.trilogy == &#39;Alien&#39; || datum.trilogy == &#39;Jaws&#39; || datum.trilogy == &#39;Mighty Ducks&#39; &quot;<br>    }<br>  ],<br>  &quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {...},<br>      &quot;encoding&quot;: {<br>        &quot;x&quot;: {...},<br>        &quot;y&quot;: {...},<br>        &quot;color&quot;: {<br>          &quot;field&quot;: &quot;trilogy&quot;<br>        }<br>      }<br>    }<br>  ]<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*VCSiIWAfKf1FmyE39jQBwQ.png" /></figure><p><strong>Create small multiples:</strong></p><p>In order to create small multiples (aka Trellis Plot), we use an express in Vega/Vega-Lite known as <a href="https://vega.github.io/vega-lite/docs/facet.html"><em>facet</em></a><em>. </em>In simple-ish terms, facets allow our plots or charts to be displayed into subsets of the same data. In this case, our subsets are values from our trilogy field. Things will shape up rather quickly now… hold on tight!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*5z6MTpEf9nLZH2xCuawqRA.gif" /></figure><p>There are two parts to the facet: <br>1. facet by row (vertically) or column (horizontally)<br>2. the chart specification (spec or view)</p><pre>// general format<br><br>&quot;facet&quot;: {<br>    &quot;column&quot;: {                      // &lt;&lt;-- the facet type (column or row)<br>      &quot;field&quot;: &quot;trilogy&quot;,            // &lt;&lt;-- the facetted field<br>      &quot;title&quot;: &quot;THE TRILOGY METER&quot;   // &lt;&lt;-- a title for our facetted chart<br>    }<br>  },<br>&quot;spec&quot;: {&quot;layer&quot;: [...]}                         // &lt;&lt;-- the specification<br></pre><p>The easiest way to amend our Vega-Lite code at this juncture is to use the trusted copy-and-paste technique:</p><pre>{<br>  &quot;$schema&quot;: &quot;https://vega.github.io/schema/vega-lite/v5.json&quot;,<br>  &quot;description&quot;: &quot;A faceted chart showing movie ratings across different franchises.&quot;,<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;height&quot;: 80,<br>  &quot;width&quot;: 95,<br>  &quot;transform&quot;: [<br>    {<br>      &quot;filter&quot;: &quot;datum.trilogy == &#39;Alien&#39; || datum.trilogy == &#39;Jaws&#39; || datum.trilogy == &#39;Mighty Ducks&#39; &quot;<br>    }<br>  ],<br>  &quot;facet&quot;: {<br>    &quot;column&quot;: {<br>      &quot;field&quot;: &quot;trilogy&quot;,<br>      &quot;title&quot;: &quot;THE TRILOGY METER&quot;<br>    }<br>  },<br>  &quot;spec&quot;: {                              // &lt;&lt;--- START SPEC<br>    &quot;layer&quot;: [                           // &lt;&lt;--- copy and paste layer within &quot;spec&quot;: {}<br>      {<br>        &quot;mark&quot;: {<br>          &quot;type&quot;: &quot;bar&quot;,<br>          &quot;stroke&quot;: &quot;#444&quot;,<br>          &quot;strokeWidth&quot;: 0.5,<br>          &quot;strokeOpacity&quot;: 0.2<br>        },<br>        &quot;encoding&quot;: {<br>          &quot;x&quot;: {<br>            &quot;field&quot;: &quot;film&quot;,<br>            &quot;type&quot;: &quot;ordinal&quot;,<br>            &quot;title&quot;: null,<br>            &quot;axis&quot;: {<br>              &quot;labels&quot;: true,<br>              &quot;labelAngle&quot;: 0,<br>              &quot;ticks&quot;: false,<br>              &quot;labelFontWeight&quot;: &quot;bold&quot;<br>            }<br>          },<br>          &quot;y&quot;: {<br>            &quot;field&quot;: &quot;imdb_score&quot;,<br>            &quot;type&quot;: &quot;quantitative&quot;,<br>            &quot;aggregate&quot;: &quot;mean&quot;,<br>            &quot;title&quot;: null,<br>            &quot;scale&quot;: {&quot;domain&quot;: [0, 1]},<br>            &quot;axis&quot;: {<br>              &quot;labels&quot;: true,<br>              &quot;format&quot;: &quot;.0%&quot;,<br>              &quot;ticks&quot;: false,<br>              &quot;grid&quot;: false<br>            }<br>          },<br>          &quot;color&quot;: {&quot;field&quot;: &quot;trilogy&quot;}<br>        }<br>      }<br>    ]                                    // layer ends<br>  }                                      // &lt;&lt;--- END SPEC<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/1*vXU8K05ZFjdfuz-r7D5GBA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*OgM88AT4C6EBp-1keECo7w.gif" /><figcaption>Facet chart</figcaption></figure><p>Next we want to improve the colour scheme so that each film (1,2,3) is assigned a colour range, darker to lighter. Let’s head over to our color encoding channel.</p><p><strong>Color Schemes:</strong></p><p>Vega and Vega-Lite have a wonderful <a href="https://vega.github.io/vega/docs/schemes/">library of ready-made colour schemes</a> to choose, from categorical to sequential, and diverging to cyclical.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*FR-RTec_32X9dsinCwhtFA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/653/1*iSe0fw0zzVXeTaPLwNAVkg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/651/1*8RMVMtRuC75ytU3oipBkWg.png" /></figure><p>We apply these colour schemes by redefining the color palette as a scale range within the color channel’s <a href="https://vega.github.io/vega-lite/docs/scale.html#scheme">scheme property</a>.</p><pre>&quot;color&quot;: {                                  // &lt;&lt;-- colour channel<br>            &quot;scale&quot;: {                      // &lt;&lt;-- colour scale<br>              &quot;scheme&quot;: &quot;yellowgreenblue&quot;   // &lt;&lt;-- colour scheme<br>            },<br>            &quot;field&quot;: &quot;imdb_score&quot;,          // apply this to the imdb_score field<br>            &quot;type&quot;: &quot;nominal&quot;,<br>            &quot;legend&quot;: null<br>          }</pre><p>and in context:</p><pre>{<br>  &quot;$schema&quot;: &quot;https://vega.github.io/schema/vega-lite/v5.json&quot;,<br>  &quot;description&quot;: &quot;A faceted chart showing movie ratings across different franchises.&quot;,<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;height&quot;: 80,<br>  &quot;width&quot;: 95,<br>  &quot;transform&quot;: [<br>    {<br>      &quot;filter&quot;: &quot;datum.trilogy == &#39;Alien&#39; || datum.trilogy == &#39;Jaws&#39; || datum.trilogy == &#39;Mighty Ducks&#39; &quot;<br>    }<br>  ],<br>  &quot;facet&quot;: {<br>    &quot;column&quot;: {<br>      &quot;field&quot;: &quot;trilogy&quot;,<br>      &quot;title&quot;: &quot;THE TRILOGY METER&quot;<br>    }<br>  },<br>  &quot;spec&quot;: {<br>    &quot;layer&quot;: [<br>      {<br>        &quot;mark&quot;: {...},                    // &lt;-- mark property<br>        &quot;encoding&quot;: {                     // &lt;-- encoding channel<br>          &quot;x&quot;: {...},<br>          &quot;y&quot;: {...},<br>          &quot;color&quot;: {                      // &lt;-- colour channel                 <br>            &quot;scale&quot;: {                    // &lt;-- colour scale<br>              &quot;scheme&quot;: &quot;yellowgreenblue&quot; // &lt;-- colour scheme<br>            },<br>            &quot;field&quot;: &quot;imdb_score&quot;,        // &lt;-- apply colour scheme to field<br>            &quot;type&quot;: &quot;nominal&quot;,<br>            &quot;legend&quot;: null                // &lt;-- set legend to null to remove<br>          }<br>        }<br>      }<br>    ]<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/662/1*_b2wRY8r2FwkENC6qHZ1RQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*dya5BRIdVN-jyiCShghJng.gif" /></figure><p>You probably feeling something right now, something tingling at the end of the finger-tips. This is called “winning”, this is what winning feels like. Keep going! 🤓🧙🏼‍♂️🪄</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/244/1*YMkZhxSrTGlTA-_g2LjjMg.gif" /><figcaption>Winning Sensation</figcaption></figure><p>We are almost, almost there. We just need to make some further aesthetic changes, we want to make the charts neat and compact. Let’s blitz through those changes here.</p><h4>Denebers of the Lost Ark</h4><p>We want to create a box area around each bar to create the illusion of filling the bar out of 100%. This is actually quite simple… follow me!</p><p>First we need to create a new field inside the Vega-Lite specification, much like a measure or a calculated column, so each bar is ‘encased’ by the maximum value. So we add an additional calculate transformation to our transform block:</p><pre>  &quot;transform&quot;: [<br>    {<br>      &quot;filter&quot;: &quot;datum.trilogy == &#39;Alien&#39; || datum.trilogy == &#39;Jaws&#39; || datum.trilogy == &#39;Mighty Ducks&#39; &quot;<br>    },<br>    {&quot;calculate&quot;: &quot;1&quot;, &quot;as&quot;: &quot;max&quot;}  // &lt;-- new calculate transform<br>  ],</pre><p>and a new mark object:</p><pre>{<br>        &quot;mark&quot;: {<br>          &quot;type&quot;: &quot;bar&quot;,<br>          &quot;color&quot;: &quot;transparent&quot;,  // &lt;-- no colour used<br>          &quot;stroke&quot;: &quot;black&quot;        // &lt;-- apply a black outline<br>        },<br>        &quot;encoding&quot;: {<br>          &quot;x&quot;: {<br>            &quot;field&quot;: &quot;film&quot;,<br>            &quot;type&quot;: &quot;ordinal&quot;,<br>            &quot;title&quot;: null,<br>            &quot;axis&quot;: {<br>              &quot;labels&quot;: true,<br>              &quot;labelAngle&quot;: 0,<br>              &quot;ticks&quot;: false<br>            }<br>          },<br>          &quot;y&quot;: {<br>            &quot;field&quot;: &quot;max&quot;,          // &lt;-- use the calculate transform field<br>            &quot;type&quot;: &quot;quantitative&quot;<br>          }<br>        }<br>      }</pre><p>…and the complete specification:</p><pre>{<br>  &quot;$schema&quot;: &quot;https://vega.github.io/schema/vega-lite/v5.json&quot;,<br>  &quot;description&quot;: &quot;A faceted chart showing movie ratings across different franchises.&quot;,<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;transform&quot;: [<br>    {<br>      &quot;filter&quot;: &quot;datum.trilogy == &#39;Alien&#39; || datum.trilogy == &#39;Jaws&#39; || datum.trilogy == &#39;Mighty Ducks&#39; &quot;<br>    },<br>    {&quot;calculate&quot;: &quot;1&quot;, &quot;as&quot;: &quot;max&quot;}<br>  ],<br>  &quot;facet&quot;: {<br>    &quot;column&quot;: {<br>      &quot;field&quot;: &quot;trilogy&quot;,<br>      &quot;title&quot;: &quot;THE TRILOGY METER&quot;<br>    }<br>  },<br>  &quot;spec&quot;: {<br>    &quot;height&quot;: 80,<br>    &quot;width&quot;: 95,<br>    &quot;layer&quot;: [<br>      {<br>        &quot;mark&quot;: {<br>          &quot;type&quot;: &quot;bar&quot;,<br>          &quot;stroke&quot;: &quot;#444&quot;,<br>          &quot;strokeWidth&quot;: 0.5,<br>          &quot;strokeOpacity&quot;: 0.2<br>        },<br>        &quot;encoding&quot;: {<br>          &quot;x&quot;: {<br>            &quot;field&quot;: &quot;film&quot;,<br>            &quot;type&quot;: &quot;ordinal&quot;,<br>            &quot;title&quot;: null,<br>            &quot;axis&quot;: {<br>              &quot;labels&quot;: true,<br>              &quot;labelAngle&quot;: 0,<br>              &quot;ticks&quot;: false,<br>              &quot;labelFontWeight&quot;: &quot;bold&quot;<br>            }<br>          },<br>          &quot;y&quot;: {<br>            &quot;field&quot;: &quot;imdb_score&quot;,<br>            &quot;type&quot;: &quot;quantitative&quot;,<br>            &quot;aggregate&quot;: &quot;mean&quot;,<br>            &quot;title&quot;: null,<br>            &quot;scale&quot;: {&quot;domain&quot;: [0, 1]},<br>            &quot;axis&quot;: {<br>              &quot;labels&quot;: true,<br>              &quot;format&quot;: &quot;.0%&quot;,<br>              &quot;ticks&quot;: false,<br>              &quot;grid&quot;: false<br>            }<br>          },<br>          &quot;color&quot;: {<br>            &quot;scale&quot;: {<br>              &quot;scheme&quot;: &quot;yellowgreenblue&quot;<br>            },<br>            &quot;field&quot;: &quot;imdb_score&quot;,<br>            &quot;type&quot;: &quot;nominal&quot;,<br>            &quot;legend&quot;: null<br>          }<br>        }<br>      },<br>      {<br>        &quot;mark&quot;: {<br>          &quot;type&quot;: &quot;bar&quot;,<br>          &quot;color&quot;: &quot;transparent&quot;,<br>          &quot;stroke&quot;: &quot;black&quot;<br>        },<br>        &quot;encoding&quot;: {<br>          &quot;x&quot;: {<br>            &quot;field&quot;: &quot;film&quot;,<br>            &quot;type&quot;: &quot;ordinal&quot;,<br>            &quot;title&quot;: null,<br>            &quot;axis&quot;: {<br>              &quot;labels&quot;: true,<br>              &quot;labelAngle&quot;: 0,<br>              &quot;ticks&quot;: false<br>            }<br>          },<br>          &quot;y&quot;: {<br>            &quot;field&quot;: &quot;max&quot;,<br>            &quot;type&quot;: &quot;quantitative&quot;<br>          }<br>        }<br>      }<br>    ]<br>  }<br>}</pre><p>This is already looking really nice:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9aBynTf_DqICDpFWg-c1TA.png" /></figure><p><strong>Next, I want to do three things:</strong></p><ol><li>remove the percentage labels</li><li>have the film numbers appear inside each bar</li><li>remove the spaces between the bars</li></ol><p>Remove percent labels on y-axis:</p><pre>{<br>  &quot;$schema&quot;: &quot;https://vega.github.io/schema/vega-lite/v5.json&quot;,<br>  &quot;description&quot;: &quot;A faceted chart showing movie ratings across different franchises.&quot;,<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;transform&quot;: [<br>    {<br>      &quot;filter&quot;: &quot;datum.trilogy == &#39;Alien&#39; || datum.trilogy == &#39;Jaws&#39; || datum.trilogy == &#39;Mighty Ducks&#39; &quot;<br>    },<br>    {&quot;calculate&quot;: &quot;1&quot;, &quot;as&quot;: &quot;max&quot;}<br>  ],<br>  &quot;facet&quot;: {<br>    &quot;column&quot;: {<br>      &quot;field&quot;: &quot;trilogy&quot;,<br>      &quot;title&quot;: &quot;THE TRILOGY METER&quot;<br>    }<br>  },<br>  &quot;spec&quot;: {<br>    &quot;height&quot;: 80,<br>    &quot;width&quot;: 95,<br>    &quot;layer&quot;: [<br>      {<br>        &quot;mark&quot;: {<br>          &quot;type&quot;: &quot;bar&quot;,<br>          &quot;stroke&quot;: &quot;#444&quot;,<br>          &quot;strokeWidth&quot;: 0.5,<br>          &quot;strokeOpacity&quot;: 0.2<br>        },<br>        &quot;encoding&quot;: {<br>          &quot;x&quot;: {<br>            &quot;field&quot;: &quot;film&quot;,<br>            &quot;type&quot;: &quot;ordinal&quot;,<br>            &quot;title&quot;: null,<br>            &quot;axis&quot;: {<br>              &quot;labels&quot;: false,            // &lt;--- set x-axis labels to &#39;false&#39;<br>              &quot;labelAngle&quot;: 0,<br>              &quot;ticks&quot;: false,<br>              &quot;labelFontWeight&quot;: &quot;bold&quot;<br>            },<br>            &quot;scale&quot;: {&quot;paddingInner&quot;: 0}<br>          },<br>          &quot;y&quot;: {<br>            &quot;field&quot;: &quot;imdb_score&quot;,<br>            &quot;type&quot;: &quot;quantitative&quot;,<br>            &quot;aggregate&quot;: &quot;mean&quot;,<br>            &quot;title&quot;: null,<br>            &quot;scale&quot;: {&quot;domain&quot;: [0, 1]},<br>            &quot;axis&quot;: {<br>              &quot;labels&quot;: false,            // &lt;--- set y-axis labels to &#39;false&#39;<br>              &quot;format&quot;: &quot;.0%&quot;,<br>              &quot;ticks&quot;: false,<br>              &quot;grid&quot;: false<br>            }<br>          },<br>          &quot;color&quot;: {<br>            &quot;scale&quot;: {<br>              &quot;scheme&quot;: &quot;yellowgreenblue&quot;<br>            },<br>            &quot;field&quot;: &quot;imdb_score&quot;,<br>            &quot;type&quot;: &quot;nominal&quot;,<br>            &quot;legend&quot;: null<br>          }<br>        }<br>      },<br>      {<br>        &quot;mark&quot;: {<br>          &quot;type&quot;: &quot;bar&quot;,<br>          &quot;color&quot;: &quot;transparent&quot;,<br>          &quot;stroke&quot;: &quot;black&quot;<br>        },<br>        &quot;encoding&quot;: {<br>          &quot;x&quot;: {<br>            &quot;field&quot;: &quot;film&quot;,<br>            &quot;type&quot;: &quot;ordinal&quot;,<br>            &quot;title&quot;: null,<br>            &quot;axis&quot;: {<br>              &quot;labels&quot;: true,<br>              &quot;labelAngle&quot;: 0,<br>              &quot;ticks&quot;: false<br>            }<br>          },<br>          &quot;y&quot;: {<br>            &quot;field&quot;: &quot;max&quot;,<br>            &quot;type&quot;: &quot;quantitative&quot;<br>          }<br>        }<br>      },<br>      {<br>        &quot;mark&quot;: {<br>          &quot;type&quot;: &quot;text&quot;,                // &lt;--- text mark (background to create the &#39;halo&#39; effect)<br>          &quot;stroke&quot;: &quot;black&quot;,<br>          &quot;strokeWidth&quot;: 3,<br>          &quot;fontSize&quot;: 14<br>        },<br>        &quot;encoding&quot;: {<br>          &quot;x&quot;: {<br>            &quot;field&quot;: &quot;film&quot;,<br>            &quot;type&quot;: &quot;ordinal&quot;,<br>            &quot;title&quot;: null,<br>            &quot;axis&quot;: {<br>              &quot;labels&quot;: true,<br>              &quot;labelAngle&quot;: 0,<br>              &quot;ticks&quot;: false<br>            }<br>          },<br>          &quot;y&quot;: {<br>            &quot;datum&quot;: &quot;0.1&quot;,<br>            &quot;type&quot;: &quot;quantitative&quot;<br>          },<br>          &quot;text&quot;: {&quot;field&quot;: &quot;film&quot;}<br>        }<br>      },<br>      {<br>        &quot;mark&quot;: {<br>          &quot;type&quot;: &quot;text&quot;,                // &lt;--- text mark foreground<br>          &quot;fill&quot;: &quot;white&quot;,<br>          &quot;fontSize&quot;: 14<br>        },<br>        &quot;encoding&quot;: {<br>          &quot;x&quot;: {<br>            &quot;field&quot;: &quot;film&quot;,<br>            &quot;type&quot;: &quot;ordinal&quot;,<br>            &quot;title&quot;: null,<br>            &quot;axis&quot;: {<br>              &quot;labels&quot;: true,<br>              &quot;labelAngle&quot;: 0,<br>              &quot;ticks&quot;: false<br>            }<br>          },<br>          &quot;y&quot;: {<br>            &quot;datum&quot;: &quot;0.1&quot;,<br>            &quot;type&quot;: &quot;quantitative&quot;<br>          },<br>          &quot;text&quot;: {&quot;field&quot;: &quot;film&quot;}    // &lt;--- display the &#39;film&#39; field values<br>        }<br>      }<br>    ]<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bWefaEa-uyHjOtLVt4P5zw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*kBq1dXUkukBQVNrxoqQO-g.gif" /></figure><p>Delightful😏… We are very close to completing the task at hand!</p><p>Rightio, now — I want to see all my films, so we can remove the filter from our transform block.</p><pre>&quot;transform&quot;: [<br>// delete this filter transform<br>    {<br>      &quot;filter&quot;: &quot;datum.trilogy == &#39;Alien&#39; || datum.trilogy == &#39;Jaws&#39; || datum.trilogy == &#39;Mighty Ducks&#39; &quot;<br>    },<br>    {&quot;calculate&quot;: &quot;1&quot;, &quot;as&quot;: &quot;max&quot;}<br>  ]</pre><p>so we are left with this:</p><pre>&quot;transform&quot;: [<br>    {&quot;calculate&quot;: &quot;1&quot;, &quot;as&quot;: &quot;max&quot;}<br>  ]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SYlSkRVfXa9Gbh4N2aJzcg.png" /></figure><p>You’ll notice all our films are beautifully facetted into the small multiples that we desire, but it’s all on a single row, we need wrap the small multiples on to a new row dynamically… hmmn, sounds tricky, but it’s not too bad… let’s take a look 👀</p><p><strong>Enter VCONCAT</strong></p><p>So we wrapped our layer in a spec, now we <a href="https://vega.github.io/vega-lite/docs/concat.html#concat">wrap our facet</a> and “spec”:{} in “vconcat”:[]</p><p>Overview:</p><pre>&quot;vconcat&quot;:[<br>  {<br>    &quot;columns&quot;: 10,    // &lt;&lt;-- the number of columns to facet by before vertically concatenating<br>    &quot;facet&quot;: {...},<br>    &quot;spec&quot;: {<br>      &quot;layer&quot;: [{...}]<br>    }<br>  }<br>]</pre><p>…and the final specification:</p><pre>{<br>  &quot;$schema&quot;: &quot;https://vega.github.io/schema/vega-lite/v5.json&quot;,<br>  &quot;description&quot;: &quot;A faceted chart showing movie ratings across different franchises.&quot;,<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;transform&quot;: [<br>    {&quot;calculate&quot;: &quot;1&quot;, &quot;as&quot;: &quot;max&quot;}<br>  ],<br>  &quot;vconcat&quot;: [<br>    {<br>      &quot;columns&quot;: 15,<br>      &quot;facet&quot;: {<br>        &quot;field&quot;: &quot;trilogy&quot;,<br>        &quot;header&quot;: {<br>          &quot;title&quot;: &quot;THE TRILOGY METER&quot;,<br>          &quot;titleFontSize&quot;: 30,<br>          &quot;titlePadding&quot;: 20,<br>          &quot;labelPadding&quot;: 0,<br>          &quot;labelFontWeight&quot;: &quot;bold&quot;,<br>          &quot;labelFontSize&quot;: 12,<br>          &quot;labelAlign&quot;: &quot;center&quot;,<br>          &quot;labelLimit&quot;: 95,<br>          &quot;labelOrient&quot;: &quot;top&quot;<br>        }<br>      },<br>      &quot;spec&quot;: {<br>        &quot;height&quot;: 80,<br>        &quot;width&quot;: 95,<br>        &quot;layer&quot;: [<br>          {<br>            &quot;mark&quot;: {<br>              &quot;type&quot;: &quot;bar&quot;,<br>              &quot;stroke&quot;: &quot;#444&quot;,<br>              &quot;strokeWidth&quot;: 0.5,<br>              &quot;strokeOpacity&quot;: 0.2<br>            },<br>            &quot;encoding&quot;: {<br>              &quot;x&quot;: {<br>                &quot;field&quot;: &quot;film&quot;,<br>                &quot;type&quot;: &quot;ordinal&quot;,<br>                &quot;title&quot;: null,<br>                &quot;axis&quot;: {<br>                  &quot;labels&quot;: false,<br>                  &quot;labelAngle&quot;: 0,<br>                  &quot;ticks&quot;: false,<br>                  &quot;labelFontWeight&quot;: &quot;bold&quot;,<br>                  &quot;domain&quot;: false<br>                },<br>                &quot;scale&quot;: {<br>                  &quot;paddingInner&quot;: 0<br>                }<br>              },<br>              &quot;y&quot;: {<br>                &quot;field&quot;: &quot;imdb_score&quot;,<br>                &quot;type&quot;: &quot;quantitative&quot;,<br>                &quot;aggregate&quot;: &quot;mean&quot;,<br>                &quot;title&quot;: null,<br>                &quot;scale&quot;: {<br>                  &quot;domain&quot;: [0, 1]<br>                },<br>                &quot;axis&quot;: {<br>                  &quot;labels&quot;: false,<br>                  &quot;format&quot;: &quot;.0%&quot;,<br>                  &quot;ticks&quot;: false,<br>                  &quot;grid&quot;: false<br>                }<br>              },<br>              &quot;color&quot;: {<br>                &quot;scale&quot;: {<br>                  &quot;scheme&quot;: &quot;yellowgreenblue&quot;<br>                },<br>                &quot;field&quot;: &quot;imdb_score&quot;,<br>                &quot;type&quot;: &quot;nominal&quot;,<br>                &quot;legend&quot;: null<br>              }<br>            }<br>          },<br>          {<br>            &quot;mark&quot;: {<br>              &quot;type&quot;: &quot;bar&quot;,<br>              &quot;color&quot;: &quot;transparent&quot;,<br>              &quot;stroke&quot;: &quot;black&quot;<br>            },<br>            &quot;encoding&quot;: {<br>              &quot;x&quot;: {<br>                &quot;field&quot;: &quot;film&quot;,<br>                &quot;type&quot;: &quot;ordinal&quot;,<br>                &quot;title&quot;: null,<br>                &quot;axis&quot;: {<br>                  &quot;labels&quot;: true,<br>                  &quot;labelAngle&quot;: 0,<br>                  &quot;ticks&quot;: false<br>                }<br>              },<br>              &quot;y&quot;: {<br>                &quot;field&quot;: &quot;max&quot;,<br>                &quot;type&quot;: &quot;quantitative&quot;<br>              }<br>            }<br>          },<br>          {<br>            &quot;mark&quot;: {<br>              &quot;type&quot;: &quot;text&quot;,<br>              &quot;stroke&quot;: &quot;black&quot;,<br>              &quot;strokeWidth&quot;: 3,<br>              &quot;fontSize&quot;: 14<br>            },<br>            &quot;encoding&quot;: {<br>              &quot;x&quot;: {<br>                &quot;field&quot;: &quot;film&quot;,<br>                &quot;type&quot;: &quot;ordinal&quot;,<br>                &quot;title&quot;: null,<br>                &quot;axis&quot;: {<br>                  &quot;labels&quot;: true,<br>                  &quot;labelAngle&quot;: 0,<br>                  &quot;ticks&quot;: false<br>                }<br>              },<br>              &quot;y&quot;: {<br>                &quot;datum&quot;: &quot;0.1&quot;,<br>                &quot;type&quot;: &quot;quantitative&quot;<br>              },<br>              &quot;text&quot;: {&quot;field&quot;: &quot;film&quot;}<br>            }<br>          },<br>          {<br>            &quot;mark&quot;: {<br>              &quot;type&quot;: &quot;text&quot;,<br>              &quot;fill&quot;: &quot;white&quot;,<br>              &quot;fontSize&quot;: 14<br>            },<br>            &quot;encoding&quot;: {<br>              &quot;x&quot;: {<br>                &quot;field&quot;: &quot;film&quot;,<br>                &quot;type&quot;: &quot;ordinal&quot;,<br>                &quot;title&quot;: null,<br>                &quot;axis&quot;: {<br>                  &quot;labels&quot;: true,<br>                  &quot;labelAngle&quot;: 0,<br>                  &quot;ticks&quot;: false<br>                }<br>              },<br>              &quot;y&quot;: {<br>                &quot;datum&quot;: &quot;0.1&quot;,<br>                &quot;type&quot;: &quot;quantitative&quot;<br>              },<br>              &quot;text&quot;: {&quot;field&quot;: &quot;film&quot;}<br>            }<br>          }<br>        ]<br>      }<br>    }<br>  ]<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VVV6MQcApGKtohEOp1tpwg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*3o9JSPHcmokgTOM9np11RQ.gif" /></figure><p>Take a bow… you’ve created something beautiful. Download the .pbix below for the complete breakdown of the steps above, including some bonus examples.</p><p>I hope you found this episode useful. Until next time… <strong>#StayQueryous</strong></p><p>🔗<strong>Github link to PBIX: </strong><a href="https://github.com/PBIQueryous/Deneb/blob/main/Medium-VegaLite-Series/EP08%20Deneb%20VegaLite%20Series%20-%20Trilogy.pbix"><strong>EP08 — Trilogy IMDB.pbix</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*URMQxNOaWlLwolWdwTjv9Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*9FpzJOd48xAuAGCO_tsKZA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0e8d5f7f42b1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deneb & Vega-Lite Walkthrough Series | EP07: NUMBER FORMATS]]></title>
            <link>https://pbi-datavizzle.medium.com/deneb-vega-lite-walkthrough-series-ep07-number-formats-5ef46090d765?source=rss-23e0d5d396ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/5ef46090d765</guid>
            <category><![CDATA[power-bi]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[power-bi-tutorials]]></category>
            <category><![CDATA[deneb]]></category>
            <category><![CDATA[vega-lite]]></category>
            <dc:creator><![CDATA[PBI DataVizzle]]></dc:creator>
            <pubDate>Mon, 01 Apr 2024 19:11:58 GMT</pubDate>
            <atom:updated>2024-04-06T04:33:04.005Z</atom:updated>
            <content:encoded><![CDATA[<h4>Numbers need some attention, and just like DAX dynamic number formatting, in Vega-Lite we have plenty of options🕊️ 🧙🏼‍♂️ ✨</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*np4uF0xj5OyC7un0SKTCMg.png" /></figure><p>💌 <em>PBIX file available at the end of the article… Enjoy!</em></p><h3>Recap</h3><ul><li><a href="https://medium.com/@pbiqueryous/power-bi-deneb-vega-lite-series-ep01-marks-and-encoding-lets-learn-to-run-before-we-fly-87f4961a2fa5">EP1</a> — Marks and Encoding</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-series-ep02-mark-types-cd7a99d4a23c">EP2</a> — Mark Types</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walk-through-series-ep03-styling-mark-properties-4dddb9e0f353">EP3</a> — Styling Mark Propreties (Part 1)</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep04-styling-mark-properties-pt2-6cafb8a2e552">EP4</a> — Styling Mark Properties (Part 2)</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep05-layers-4ab67067f717">EP5</a> — Layers (Multiple Views)</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep06-expressions-conditional-formatting-pt1-eab3ca34f0de">EP6</a> — Expressions &amp; Conditional Formatting</li></ul><h3>Default Number Formatting</h3><p>In Vega-Lite, numerical fields are not formatted — which means they are a blank canvas for us to paint and shape as our heart desires 🥰</p><p>In Vega-Lite, you can apply formatting options in various properties such as <strong>axis labels</strong>, <strong>tooltip content</strong>, and <strong>text marks</strong>. By specifying the format property, you can control how numeric data is displayed, enhancing the readability and effectiveness of your visualizations For this episode we will focus on the latter, but the principles and techniques for formatting axis labels and tooltip content are rather similar.</p><h3>D3 Number Formatting Basics:</h3><h4>Fixed-point notation (f):</h4><p>Displays numbers as fixed-point notation. The precision specifier indicates the number of digits after the decimal point.</p><ul><li>Example: &quot;.2f&quot; for 123.456 → &quot;123.46&quot;</li></ul><h4>SI-prefix (s):</h4><p>Uses SI prefix for the given number and precision, useful for large or small numbers.</p><ul><li>Example: &quot;,.3s&quot; for 123456789 → &quot;123M&quot;</li></ul><h4>Percentage (%):</h4><p>Multiplies the number by 100 and displays it in fixed-point notation, followed by a percent sign. The precision specifier indicates the number of digits after the decimal point.</p><ul><li>Example: &quot;.1%&quot; for 0.123 → &quot;12.3%&quot;</li></ul><h4>Comma-separated (,f):</h4><p>Formats the number with commas as thousands separators in fixed-point notation.</p><ul><li>Example: &quot;,.0f&quot; for 1234567 → &quot;1,234,567&quot;</li></ul><h4>Rounded notation (r):</h4><p>Rounds numbers to significant digits. The precision specifier indicates the number of significant digits.</p><ul><li>Example: &quot;,.3r&quot; for 12345 → &quot;12,300&quot;</li></ul><h4>Exponential notation (e):</h4><p>Displays numbers in exponential notation. The precision specifier indicates the number of digits after the decimal point.</p><ul><li>Example: &quot;.2e&quot; for 123.456 → &quot;1.23e+2&quot;</li></ul><h4>General notation (g):</h4><p>Uses either fixed-point or exponential notation, whichever is more compact, for the given number and precision.</p><ul><li>Example: &quot;0.2g&quot; for 123.456 → &quot;1e+2&quot;</li></ul><p>Please… you don’t need to say it… this isn’t an April Fool’s Trick, this is actually how some people choose to live in the D3 world. 😅 Alas, it’s our world now! 😋</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*9R9dffRdM5GsUBxGhRoQAg.gif" /></figure><p>Joking aside, this isn’t about hating, it’s about contemplating 🤓 — D3 number formatting allows for much needed dynamism and precision, but the question is how can we do this ✨better✨?… Come with me!</p><p>Now we can do a lot with very little. So let’s start with the basics:</p><h4>Thousands Separated 1.0</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/178/1*pqcH8KlLz9VefyGC4yIzew.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/399/1*gQnWKXz-aNJaIt5JAv2nqQ.png" /></figure><pre>{<br>  &quot;title&quot;: &quot;Thousands Separator&quot;,<br>  &quot;mark&quot;: { &quot;type&quot;: &quot;text&quot; },  // text mark<br>  &quot;encoding&quot;: {<br>    &quot;text&quot;: {                  //&lt;-- text encoding channel<br>      &quot;field&quot;: &quot;Value&quot;,        // dataset field<br>      &quot;type&quot;: &quot;quantitative&quot;,  // quantitative type <br>      &quot;format&quot;: &quot;,&quot;            // standard thousand separator formatting<br>    },<br>    &quot;y&quot;: {...}<br>  }<br>}</pre><h4>Thousand Separated 2.0</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/138/1*CWZMt34FrSo7fsPzVNOkrg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/399/1*gQnWKXz-aNJaIt5JAv2nqQ.png" /></figure><pre>{<br>  &quot;title&quot;: &quot;Zero Decimal (with Thousands Separator)&quot;,<br>  &quot;mark&quot;: { &quot;type&quot;: &quot;text&quot; },  // text mark<br>  &quot;encoding&quot;: {<br>    &quot;text&quot;: {                  //&lt;-- text encoding channel<br>      &quot;field&quot;: &quot;Value&quot;,        // dataset field<br>      &quot;type&quot;: &quot;quantitative&quot;,  <br>      &quot;format&quot;: &quot;,.0f&quot;        // zero decimal, comma separated<br>    },<br>    &quot;y&quot;: {...}<br>  }<br>}</pre><h4>SI Prefix (Significant Digits) 1.0</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/88/1*BTQnxjRyU8eUZvGohk427g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/399/1*gQnWKXz-aNJaIt5JAv2nqQ.png" /></figure><pre>{<br>  &quot;title&quot;: &quot;SI Prefix (3 Significant Digits)&quot;,<br>  &quot;mark&quot;: { &quot;type&quot;: &quot;text&quot; },  // text mark<br>  &quot;encoding&quot;: {<br>    &quot;text&quot;: {                  //&lt;-- text encoding channel<br>      &quot;field&quot;: &quot;Value&quot;,        // dataset field<br>      &quot;type&quot;: &quot;quantitative&quot;,  <br>      &quot;format&quot;: &quot;,.3s&quot;        // 3 significant numbers<br>    },<br>    &quot;y&quot;: {...}<br>  }<br>}</pre><h4>SI Prefix (Significant Digits) 2.0</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/80/1*2xR2qVvrNtQuCc5NEKqMqQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/399/1*gQnWKXz-aNJaIt5JAv2nqQ.png" /></figure><pre>{<br>  &quot;title&quot;: &quot;SI Prefix (3 Significant Digits)&quot;,<br>  &quot;mark&quot;: { &quot;type&quot;: &quot;text&quot; },  // text mark<br>  &quot;encoding&quot;: {<br>    &quot;text&quot;: {                  //&lt;-- text encoding channel<br>      &quot;field&quot;: &quot;Value&quot;,        // dataset field<br>      &quot;type&quot;: &quot;quantitative&quot;,  <br>      &quot;format&quot;: &quot;,.3~s&quot;        // 3 significant numbers, removing trailing zeros<br>    },<br>    &quot;y&quot;: {...}<br>  }<br>}</pre><h4>Rounded (3 Significant Digits)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/123/1*5IieK7zsiqnxKdQEaJkKiQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/399/1*gQnWKXz-aNJaIt5JAv2nqQ.png" /></figure><pre>{<br>  &quot;title&quot;: &quot;Rounded (3 Significant Digits)&quot;,<br>  &quot;mark&quot;: { &quot;type&quot;: &quot;text&quot; },  // text mark<br>  &quot;encoding&quot;: {<br>    &quot;text&quot;: {                  //&lt;-- text encoding channel<br>      &quot;field&quot;: &quot;Value&quot;,        // dataset field<br>      &quot;type&quot;: &quot;quantitative&quot;,  <br>      &quot;format&quot;: &quot;,.3r&quot;        // 3 significant numbers<br>    },<br>    &quot;y&quot;: {...}<br>  }<br>}</pre><p>This is great. Generally, I tend to use the “,.3~s” or “,.4~s” formatting style. But my preference is to keep the text data labels as compact as possible. Using the above technique only gets you so far, soon you stumble across the oddity of the “billions” number denomination, which looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*kZst03PMCfYXj6q3Usorug.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*qg8RX3GAVXuAo-9y1HPLug.gif" /></figure><p>Yep. We’ve entered the weird world of D3 formatting. I don’t know anyone who understands that “G” means “billions”. So we have to get advanced, we need more flexibility.</p><h3>Custom Number Formatting</h3><p>Again, we have several techniques we can employ. I’ll use a DAX-inspired method and a Vega-Lite one.</p><h4>DAX-Inspired Number Format</h4><p>This method was revealed thanks to <a href="https://medium.com/u/a180fa2d85ee">Kurt Buhler</a> 🧙🏽‍♂️🪄. In his recent <a href="https://www.sqlbi.com/articles/improving-data-labels-with-format-strings/">sqlbi article</a> he demonstrated a really tidy technique using scientific / exponential notation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l98S7cCbBOImpS3sGfUS9w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/335/1*7T3jWegY560JE-yShdiOpA.png" /><figcaption>DAX format number string</figcaption></figure><p>To replicate this in Vega-Lite, we create a combination calculate transformations alongside some params:</p><pre>&quot;params&quot;: [<br>    {<br>      &quot;name&quot;: &quot;_quintillions&quot;,<br>      &quot;value&quot;: &quot;#,0,,,,,,.#&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;_quadrillions&quot;,<br>      &quot;value&quot;: &quot;#,0,,,,,.#&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;_trillions&quot;,<br>      &quot;value&quot;: &quot;#,0,,,,.#&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;_billions&quot;,<br>      &quot;value&quot;: &quot;#,0,,,.#&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;_millions&quot;,<br>      &quot;value&quot;: &quot;#,0,,.#&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;_thousands&quot;,<br>      &quot;value&quot;: &quot;#,0,.#&quot;<br>    }<br>  ],<br>&quot;transform&quot;: [<br>  {<br>    &quot;calculate&quot;: &quot;datum.Value &gt;= 5e17 ? pbiFormat(datum.Value,_quintillions) + &#39;Q&#39;  <br>    : datum.Value &gt;= 5e14 ? pbiFormat(datum.Value,_quadrillions) + &#39;q&#39;  <br>    : datum.Value &gt;= 5e11 ? pbiFormat(datum.Value,_trillions) + &#39;T&#39; <br>    : datum.Value &gt;= 5e8 ? pbiFormat(datum.Value,_billions) + &#39;B&#39; <br>    : datum.Value &gt;= 5e5 ? pbiFormat(datum.Value,_millions) + &#39;M&#39; <br>    : datum.Value &gt;= 1e3 ? pbiFormat(datum.Value,_thousands) + &#39;K&#39; <br>    : pbiFormat(datum.Value,&#39;#,#&#39;)&quot;,<br>      &quot;as&quot;: &quot;pbiformat_v0&quot;<br>  }<br>]</pre><pre>&quot;encoding&quot;: {<br>  &quot;text&quot;: {<br>    &quot;field&quot;: &quot;pbiformat_v0&quot;,<br>    &quot;type&quot;: &quot;nominal&quot;  //&lt;-- nominal because formatted numbers are text strings<br>  },<br>  &quot;y&quot;: {...}<br>}</pre><p>Not as tidy as DAX’s SWITCH() statement, but it’s pretty good!</p><p>How does it look? Champion!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/183/1*SsjMAP-keZpxd4qFjZfxHw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/474/1*PrvZjY1Q-sAv1IMdN9dEFA.gif" /></figure><p>Crisp, Clean, Concise. Let’s try another way</p><h4>DAX-Inspired Vega-Lite Adaptation Method:</h4><pre><br>&quot;transform&quot;: [<br>  {<br>    &quot;calculate&quot;: &quot;datum.Value &gt;= 1e18 ? <br>      format((datum.Value / 1e18),&#39;,.1f&#39;) + &#39;Q&#39;  <br>    : datum.Value &gt;= 1e16 ? format((datum.Value / 1e15), &#39;,.1~f&#39;) + &#39;q&#39;  <br>    : datum.Value &gt;= 1e12 ? format((datum.Value / 1e12), &#39;,.1~f&#39;) + &#39;T&#39; <br>    : datum.Value &gt;= 1e9 ? format((datum.Value / 1e9), &#39;,.1~f&#39;) + &#39;B&#39; <br>    : datum.Value &gt;= 1e6 ? format((datum.Value / 1e6), &#39;,.1~f&#39;) + &#39;M&#39; <br>    : datum.Value &gt;= 1e3 ? format((datum.Value / 1e3), &#39;,.1~f&#39;) + &#39;K&#39; <br>    : format(datum.Value, &#39;,.1~f&#39;)&quot;,<br>    &quot;as&quot;: &quot;format_v0&quot;<br>  }<br>]</pre><pre>&quot;encoding&quot;: {<br>  &quot;text&quot;: {<br>    &quot;field&quot;: &quot;format_v0&quot;,<br>    &quot;type&quot;: &quot;nominal&quot;  //&lt;-- nominal because formatted numbers are text strings<br>  },<br>  &quot;y&quot;: {...}<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/72/1*aUlmlYoyvNz9AH7m-c3cvA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*aYLIzz_UdcDW36ngQWr-Rw.gif" /></figure><h4>Hidden Option in Deneb’s pbiFormat expression</h4><p>There is also a bonus 3rd parameter for dynamic pbiFormat expression (thanks <a href="https://medium.com/u/dcfbf31b00d6">Daniel Marsh-Patrick</a>!) — read more information on this gem at the <a href="https://deneb-viz.github.io/formatting#pbiformat-expression-function-full-implementation-details">Deneb Website</a>. The 3rd parameter allows you to set a list of options, mainly the Value to format, the Precison (number of decimals) and the Culture (en-US; en-GB).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/151/1*BH3UZ7vGfEk-RVsXI-WUQg.png" /></figure><pre>//** layout is not JSON compliant - displayed for effect **//<br><br>{<br>      &quot;calculate&quot;: &quot;pbiFormat( datum.Value, &#39;#,0&#39;, <br>                   {<br>                     value: datum.Value, <br>                     precision: 1, <br>                     cultureSelector: &#39;en-GB&#39;<br>                    })&quot;, <br>      &quot;as&quot;: &quot;pbiFormat_Options&quot;<br> }</pre><pre>//** JSON proper **//<br><br>{<br>      &quot;calculate&quot;: &quot;pbiFormat( datum.Value, &#39;#,0&#39;, { value: datum.Value, precision: 1, cultureSelector: &#39;en-GB&#39;})&quot;, <br>      &quot;as&quot;: &quot;pbiFormat_Options&quot;<br> }</pre><p>And the final result, putting it into practice?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zip7VsWV-bfjNdFakXdTVQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*YM7nSp0jk2vpoxNaWd06iA.gif" /></figure><p>This was just a whistle-stop tour — thank you for staying and hope to see you again soon with more nuggets!</p><p>#StayQueryous</p><p>🔗<strong>Github link to PBIX: </strong><a href="https://github.com/PBIQueryous/Deneb/blob/main/Medium-VegaLite-Series/EP07%20Deneb%20VegaLite%20Series%20-%20Number%20Formatting.pbix"><strong>EP07 — Number Formatting.pbix</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*URMQxNOaWlLwolWdwTjv9Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/909/1*hXL6ME5ZvfpdJVTdFQqr8Q.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5ef46090d765" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deneb & Vega-Lite Walkthrough Series | EP06: EXPRESSIONS & CONDITIONAL FORMATTING (pt2)]]></title>
            <link>https://pbi-datavizzle.medium.com/deneb-vega-lite-walkthrough-series-ep06-expressions-conditional-formatting-pt2-87aefaebd5e6?source=rss-23e0d5d396ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/87aefaebd5e6</guid>
            <category><![CDATA[power-bi]]></category>
            <category><![CDATA[power-bi-tutorials]]></category>
            <category><![CDATA[data-visualisation]]></category>
            <category><![CDATA[deneb]]></category>
            <category><![CDATA[vega-lite]]></category>
            <dc:creator><![CDATA[PBI DataVizzle]]></dc:creator>
            <pubDate>Wed, 13 Mar 2024 16:40:52 GMT</pubDate>
            <atom:updated>2024-03-13T16:40:52.140Z</atom:updated>
            <content:encoded><![CDATA[<h4>Vega-Lite expressions are both flexible and powerful. They WILL change your dataviz life for the better, opening up a world of near-unlimited viz formatting possibilities. 🕊️ 🧙🏼‍♂️ ✨</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gy2oR1umlDeI1oaqHXT3bA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*np4uF0xj5OyC7un0SKTCMg.png" /></figure><p>💌 <em>PBIX file available at the end of the article… Enjoy!</em></p><h3>Recap</h3><ul><li><a href="https://medium.com/@pbiqueryous/power-bi-deneb-vega-lite-series-ep01-marks-and-encoding-lets-learn-to-run-before-we-fly-87f4961a2fa5">EP1</a> — Marks and Encoding</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-series-ep02-mark-types-cd7a99d4a23c">EP2</a> — Mark Types</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walk-through-series-ep03-styling-mark-properties-4dddb9e0f353">EP3</a> — Styling Mark Propreties (Part 1)</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep04-styling-mark-properties-pt2-6cafb8a2e552">EP4</a> — Styling Mark Properties (Part 2)</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep05-layers-4ab67067f717">EP5</a> — Layers (Multiple Views)</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep06-expressions-conditional-formatting-pt1-eab3ca34f0de">EP6 </a>— Expressions &amp; Conditional Formatting (Part 1)</li></ul><h3>Picking Up Where We Left Off</h3><p>If you haven’t been scared off by <a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep06-expressions-conditional-formatting-pt1-eab3ca34f0de">Part 1</a> 😅then I promise in Part 2, you will be cruising…easy, breezy. Power Up! 🌈</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*VdHL1zw01v4af15-9Zwd0w.gif" /></figure><h3>1. Conditional Formatting via the Encoding channel</h3><p>Previously, we achieved conditional formatting using a combination of Calculate Transforms and Expressions. In this section, we will apply the same logic, using the <a href="https://vega.github.io/vega-lite/docs/condition.html"><em>Condition</em></a><em> </em>property in the mark’s Encoding channel.</p><p>As with all learning journeys, there is always an element of pain, and that pain my friends is <strong><em>documentation</em></strong>. It hurts, but it most importantly it can definitely help. How to explain the Condition property? What does Vega-Lite say?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/738/1*l2TWknIE-5mbyhnKMYROXw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/745/1*qllScYo7u4O82FlGYDddHw.png" /></figure><p>In other words, the Conditional property is also flexible, and there are several ways in which you can achieve conditional formatting using this method. Let’s take a look at those using the PBIX file at the end of the article.</p><p>Let’s try some malpractice first, so we can better understand the VL behaviours. Then we’ll end with a refined example to finish. Below is the skeleton structure</p><p><strong>Mark Expression Method</strong></p><pre>// Expression Method<br>{                                    //&lt;-- open mark property<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;xOffset&quot;: 0,<br><br>/*----------------------------------------------------------------<br>Format the mark&#39;s colour property with an expression<br>----------------------------------------------------------------*/<br>        &quot;color&quot;: {<br>          &quot;expr&quot;: &quot;datum._isYTD == 1 ? _barColourACT : _barColourFRC&quot;<br>        },<br>//----------------------------------------------------------------<br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;strokeWidth&quot;: 1<br>      }<br>    }                              //&lt;-- close mark property<br>/* <br>• &quot;?&quot; = if the logical test true then return subsequent value...<br>• &quot;:&quot; = otherwise return subsequent value<br>• eg: &quot;logical test ? return value : return another Value&quot;<br><br>plain english: if the value in the [isYTD] field equals 1<br>then return _barColourACT otherwise return _barColourFRC */</pre><p><strong>Encoding Condition Method 1</strong></p><pre>// Encoding Condition Method<br>  &quot;encoding&quot;: {    //&lt;-- open encoding properties<br>    &quot;x&quot;: {...},<br>    &quot;y&quot;: {...},<br>    &quot;color&quot;: {        //&lt;&lt;-- open colour properties<br>      &quot;condition&quot;: [          //&lt;-- open conditions<br>        {<br>          &quot;test&quot;: {<br>            &quot;field&quot;: &quot;_isYTD&quot;,<br>            &quot;equal&quot;: 1<br>          },<br>          &quot;value&quot;: {<br>            &quot;expr&quot;: &quot;_barColourACT&quot;<br>          }<br>        }<br>      ],                     //&lt;-- close condition<br>      &quot;value&quot;: {&quot;expr&quot;: &quot;_barColourFRC&quot;}  //&lt;-- value to return if condition not met<br>    }                //&lt;&lt;-- close colour properties<br>  }              //&lt;-- close encoding properties<br>/* <br>• &quot;condition&quot; = the property which will contain our logical test...<br>• &quot;test&quot; = the elements that comprise our test<br>• &quot;field&quot; = the field we are testing against<br>• &quot;equal&quot;: this is the predicate (equal, lessthan, morethan etc)<br>• &quot;value&quot;: the result to return if the test is met/not met<br><br>plain english: if the value in the [isYTD] field equals 1<br>then return _barColourACT otherwise return _barColourFRC */</pre><p><strong>Encoding Condition Method 2</strong></p><pre>// Encoding Condition Method<br>  &quot;encoding&quot;: {    //&lt;-- open encoding properties<br>    &quot;x&quot;: {...},<br>    &quot;y&quot;: {...},<br>    &quot;color&quot;: {        //&lt;&lt;-- open colour properties<br>      &quot;condition&quot;: [      //&lt;-- open conditions<br>        {<br>          &quot;test&quot;: &quot;datum._isYTD == 1&quot;,<br>          &quot;value&quot;: {<br>            &quot;expr&quot;: &quot;_barColourACT&quot;<br>          }<br>        }<br>      ],                  //&lt;-- close condition<br>      &quot;value&quot;: {&quot;expr&quot;: &quot;_barColourFRC&quot;}  //&lt;-- value to return if condition not met<br>    }                 //&lt;&lt;-- close colour properties<br>  }               //&lt;-- close encoding properties<br>/* <br>• &quot;condition&quot; = the property which will contain our logical test...<br>• &quot;test&quot; = the logical test (similar to the mark expression method)<br>• &quot;value&quot;: the result to return if the test is met/not met<br><br>plain english: if the value in the [isYTD] field equals 1<br>then return _barColourACT otherwise return _barColourFRC */</pre><p>Now to test it in practice. Let’s simply add the color condition at the end of our encoding channel in the Spec :</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*BqZiF7NZksZABsBL2k5ZRg.png" /></figure><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;params&quot;: [],<br>  &quot;transform&quot;: [<br>    {<br>      &quot;calculate&quot;: &quot;datetime( now()) &quot;,<br>      &quot;as&quot;: &quot;_Today&quot;<br>    },<br>    {<br>      &quot;calculate&quot;: &quot;datum[&#39;Date&#39;] &lt;= datum._Today ? 1 : 0 &quot;,<br>      &quot;as&quot;: &quot;_isYTD&quot;<br>    }<br>  ],<br>  &quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;xOffset&quot;: 0,<br>/*----------------------------------------------------------------<br>Format the mark&#39;s colour property with an expression<br>NOTE: This is superfluous to requirements now, but we&#39;ll test<br>which propery has a higher precedence<br>----------------------------------------------------------------*/<br>        &quot;color&quot;: {<br>          &quot;expr&quot;: &quot;datum._isYTD == 1 ? _barColourACT : _barColourFRC&quot;<br>        },<br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;strokeWidth&quot;: 1<br>      }<br>    }<br>  ],<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;Date&quot;,<br>      &quot;type&quot;: &quot;ordinal&quot;,<br>      &quot;timeUnit&quot;: &quot;yearmonth&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;format&quot;: &quot;%b-%y&quot;<br>      }<br>    },<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;AC&quot;,<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;axis&quot;: {<br>        &quot;format&quot;: &quot;£0,,.#M&quot;,<br>        &quot;formatType&quot;: &quot;pbiFormat&quot;<br>      }<br>    },<br>/*----------------------------------------------------------------<br>Format the encoding colour property with a condition (method 2)<br>----------------------------------------------------------------*/<br>    &quot;color&quot;: {<br>      &quot;condition&quot;: [<br>        {<br>          &quot;test&quot;: &quot;datum._isYTD == 1&quot;,<br>          &quot;value&quot;: {<br>            &quot;expr&quot;: &quot;_barColourACT&quot;<br>          }<br>        }<br>      ],<br>      &quot;value&quot;: {&quot;expr&quot;: &quot;_barColourFRC&quot;}<br>    }<br>//---------------------------------------------------------------<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*hfyBdgY6jnQG2JVuW03ByA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*hy_OuzYtHljJ8R7RqCULgQ.gif" /></figure><p>Fantastic! Next, out of curiosity, I want to see which method (Mark Expression or Encoding Condition) has a higher precedence: i.e. which one has a higher priority?</p><p>To test this, let’s just change the colour of the Actual bar in the encoding condition.</p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;params&quot;: [],<br>  &quot;transform&quot;: [...],<br>  &quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;xOffset&quot;: 0,<br>/*----------------------------------------------------------------<br>Format the mark&#39;s colour property with an expression<br>NOTE: This is superfluous to requirements now, but we&#39;ll test<br>which propery has a higher precedence<br>----------------------------------------------------------------*/<br>        &quot;color&quot;: {<br>          &quot;expr&quot;: &quot;datum._isYTD == 1 ? _barColourACT : _barColourFRC&quot;<br>        },<br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;strokeWidth&quot;: 1<br>      }<br>    }<br>  ],<br>  &quot;encoding&quot;: {,<br>    &quot;x&quot;: {...},<br>    &quot;y&quot;: {...},<br>/*----------------------------------------------------------------<br>Format the encoding colour property with a condition (method 2)<br>----------------------------------------------------------------*/<br>    &quot;color&quot;: {<br>      &quot;condition&quot;: [<br>        {<br>          &quot;test&quot;: &quot;datum._isYTD == 1&quot;,<br>          &quot;value&quot;: {<br>            &quot;expr&quot;: &quot;&#39;yellow&#39;&quot; //&lt;--- make sure to use single quotes &#39;yellow&#39;<br>          }<br>        }<br>      ],<br>      &quot;value&quot;: {&quot;expr&quot;: &quot;_barColourFRC&quot;}<br>    }<br>//---------------------------------------------------------------<br>  }<br>}</pre><p>And what do we find…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/1*3wzglJYtiGcJjoVIsgI2xQ.png" /></figure><p>Exactly… the encoding color properties have a higher precedence than the mark color properties, it effectively overwrites any formatting we’ve applied to the mark color property. So, we can remove the mark color properties from our Vega-Lite code. See below:</p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;params&quot;: [],<br>  &quot;transform&quot;: [...],<br>  &quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;xOffset&quot;: 0,<br>//        &quot;color&quot;: {...}     //&lt;-- we&#39;ve deleted this section<br>    }<br>  ],<br>  &quot;encoding&quot;: {...},<br>    &quot;y&quot;: {...},<br>/*----------------------------------------------------------------<br>Format the encoding colour property with a condition (method 2)<br>----------------------------------------------------------------*/<br>    &quot;color&quot;: {<br>      &quot;condition&quot;: [<br>        {<br>          &quot;test&quot;: &quot;datum._isYTD == 1&quot;,<br>          &quot;value&quot;: {<br>            &quot;expr&quot;: &quot;&#39;yellow&#39;&quot; //&lt;--- make sure to use single quotes &#39;yellow&#39;<br>          }<br>        }<br>      ],<br>      &quot;value&quot;: {&quot;expr&quot;: &quot;_barColourFRC&quot;}<br>    }<br>//---------------------------------------------------------------<br>  }<br>}</pre><p>Now run that neater code and….behold!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*PRx8UtzRw2X_tJKJ2g3IYg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*_ba6FsrvDIqiJmjvm13LnQ.gif" /><figcaption>Encoding Condition</figcaption></figure><p>Congratulations, you now have COMPLETE control over your Deneb chart formatting!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*xtsp7cA-6zX850hgQLFwxw.gif" /></figure><p>I hope you find the article useful, thank you for following me on this journey — until next time… <strong>#StayQueryous</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*URMQxNOaWlLwolWdwTjv9Q.png" /></figure><p>🔗<strong>Github link to PBIX: </strong><a href="https://github.com/PBIQueryous/Deneb/blob/main/Medium-VegaLite-Series/EP06%20Deneb%20VegaLite%20Series%20-%20Expression%20and%20Conditional%20Formatting%20pt2.pbix"><strong>EP06 Deneb VegaLite Series pt2.pbix</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4fx6HWz3k6BjlGlJgtnlvA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87aefaebd5e6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deneb & Vega-Lite Walkthrough Series | EP06: EXPRESSIONS & CONDITIONAL FORMATTING (pt1)]]></title>
            <link>https://pbi-datavizzle.medium.com/deneb-vega-lite-walkthrough-series-ep06-expressions-conditional-formatting-pt1-eab3ca34f0de?source=rss-23e0d5d396ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/eab3ca34f0de</guid>
            <category><![CDATA[vega-lite]]></category>
            <category><![CDATA[deneb]]></category>
            <category><![CDATA[power-bi-tutorials]]></category>
            <category><![CDATA[power-bi]]></category>
            <category><![CDATA[data-visualization]]></category>
            <dc:creator><![CDATA[PBI DataVizzle]]></dc:creator>
            <pubDate>Wed, 13 Mar 2024 14:03:27 GMT</pubDate>
            <atom:updated>2024-03-13T14:03:27.905Z</atom:updated>
            <content:encoded><![CDATA[<h4>Vega-Lite expressions are both flexible and powerful. They WILL change your dataviz life for the better, opening up a world of near-unlimited viz formatting possibilities. 🕊️ 🧙🏼‍♂️ ✨</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8GuuckoutnWUPJyHOroIAQ.png" /><figcaption>Deneb (left) vs Native PBI (right)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*np4uF0xj5OyC7un0SKTCMg.png" /></figure><p>💌 <em>PBIX file available at the end of the article… Enjoy!</em></p><h4>CAUTION!⚠️ADDICTIVE CONTENT ⚠️</h4><h3>Recap</h3><ul><li><a href="https://medium.com/@pbiqueryous/power-bi-deneb-vega-lite-series-ep01-marks-and-encoding-lets-learn-to-run-before-we-fly-87f4961a2fa5">EP1</a> — Marks and Encoding</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-series-ep02-mark-types-cd7a99d4a23c">EP2</a> — Mark Types</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walk-through-series-ep03-styling-mark-properties-4dddb9e0f353">EP3</a> — Styling Mark Propreties (Part 1)</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep04-styling-mark-properties-pt2-6cafb8a2e552">EP4</a> — Styling Mark Properties (Part 2)</li><li><a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep05-layers-4ab67067f717">EP5</a> — Layers (Multiple/Composite Views)</li></ul><h3>Intro</h3><p>I am excited to write this article and I am excited for you because you will hopefully unlock your abilities to unleash next-level wizardry in your Deneb data visualisations. It might get a little intense, a little wild and confusing at times 🤪… but if we break it down into little pieces, the penny will drop for you like it did for me🥲. This episode will be the icing on the cake 🎂with the cherry 🍒 on top! 🙌🏽</p><p>There’s a lot to get through, so make sure you are hydrated and well-nourished — we need all the help we can get 😅</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pUXlc4hSZtyi50YuLxAFvw.png" /></figure><h3>Expressions</h3><p>What are expressions? 🤔Think of them as data manipulations or transformations. They have the same effect as creating a DAX measure with conditional logic for conditional formatting.</p><p>If we start by referring to the trusted <a href="https://vega.github.io/vega/docs/expressions/">Vega documentation</a>, you will soon realise you have entered a world of pain. Nested deep in the <a href="https://vega.github.io/vega/docs/expressions/"><strong>Expressions </strong></a>section, there is a small paragraph on something called <em>Control Flow Functions</em> 🤨</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/766/1*O8LXNJRGCYRp8QkwoSKeWQ.png" /></figure><p>and <a href="https://vega.github.io/vega-lite/docs/types.html#expression">elsewhere</a> we see:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/719/1*Hx2nzuZvlhLnGJLgx4RTyQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/1*bIb_FjiZJ3Yicv6Kq0nH6A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*uxijyKDYe5thBUngetSMBw.gif" /></figure><blockquote>“Control whatty what-now… Ternary Opera-whatchimacallit<em>??🤯</em></blockquote><p>To mere mortals, we are talking about IF statements! Nice, safe, easy(-ish), comfortable, familiar IF statements 🙌🏽</p><p>We’ll take a look at the DAX examples first.</p><h3>Conditional Formatting</h3><h4>DAX Examples</h4><p>Below are 2 examples of typical conditional formatting measures using the SWITCH() function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*8kmGQ_Emvw6BvGpegYnKRg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/422/1*d69kOwuvjBfETUzSUJVvVQ.png" /><figcaption>SWITCH() vs SWITCH( TRUE())</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/543/1*Q5pjXAYgSSCcZhyIb7u3bw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*qg_4OOSJQo_UPDO-2sc58g.png" /></figure><p>That is indeed… tremendous.</p><h4>Vega-Lite Examples</h4><p>Next, we’ll do the exact same with Vega-Lite in Deneb. This is a quick glance, we’ll dig into the detail shortly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/181/1*thk6DrCCaOvNJNtD0FnkKQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/657/1*8tLgPyRzMK8VgHHVq6zcxg.png" /><figcaption>In-line VL expression for conditional formatting</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/1*n5YJAssHUKZlFjKOlEA6Jg.png" /></figure><pre>&quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;xOffset&quot;: 0,<br>/* -----------------------------------------------------------------------<br>conditionally format the mark&#39;s colour property, if the column data value<br>equals 1 then return the blue colour otherwise return the grey colour<br>------------------------------------------------------------------------*/<br>        &quot;color&quot;: {<br>          &quot;expr&quot;: &quot;datum.isYTD_INT == 1 ? &#39;#004385&#39; : &#39;#EDEDED&#39;&quot;<br>        },<br>//----------------------------------------------------------------------\\<br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;strokeWidth&quot;: 1<br>      }<br>    }<br>  ]</pre><p>Can you see the similarities between the DAX and VL methods? We achieve the same result, with similar techniques, just with a different coding langauge.</p><p>This is the starting point. We eventually want to end up with this result 👇🏽</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*hgTQhljo-T_vxCGmmhQxxQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*UyfIfssxCjguiRoO0gk1fw.gif" /></figure><p>Now let’s break it down, step by step. I will show you two examples:</p><ul><li>One with Power Query “helper” columns</li><li>One using in-line VL transformations and expression, and no helper columns</li></ul><h3>1. Conditional Formatting with Power Query columns (dimension table)</h3><h4>Step 1: House Keeping — preparing variables/parameters for tidier coding</h4><p>As you will have experienced, in any conditional formatting situation, the lines of code can explode rather rapidly, which can be overwhelming to manage and maintain, and makes reading the code somewhat more difficult. We’ll organise our code in such a way as to reduce duplication and redundancy, and make it more pleasant to peruse.</p><p>To provide the foundations, we first want to copy and paste this code into the Config tab. The intention is to create a consistent theme for all chart elements in terms of fonts, axis &amp; label fonts, and X &amp; Y-axis chart design. In terms of fonts, I don’t think there is a way to set a “global” font, we have to declare the font for the various chart properties. I’ve used parameters (<a href="https://vega.github.io/vega-lite/docs/parameter.html"><em>params</em></a>), which are similar to declaring <a href="https://learn.microsoft.com/en-us/dax/best-practices/dax-variables"><em>variables</em></a><em> </em><a href="https://www.sqlbi.com/articles/variables-in-dax/">(var) in DAX</a>.</p><p>Curveball… the term <em>variable </em>is a misnomer, a variable is better understood as a <em>constant </em>or a <em>parameter </em>🫠</p><blockquote>You can think about a variable as a name for an expression. The term “variable” itself is somewhat misleading — a DAX variable cannot change its value, as the name would suggest. <strong>A DAX variable is indeed a constant</strong>, meaning that it is a name for the value computed by the definition of the variable.</blockquote><blockquote>— SQLBI (Marco Russo &amp; Alberto Ferrari)</blockquote><p>Back to the lesson…paste the code sample below into the Config tab. I’ve added annotation for illustrative purposes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*X5tOfPZtNDFJJmvqn8ywcw.png" /></figure><pre>{<br>  &quot;view&quot;: {&quot;stroke&quot;: &quot;transparent&quot;},<br>/*------------------------------------------------------------------<br>Declare parameters/variables<br>------------------------------------------------------------------*/<br>  &quot;params&quot;: [<br>    {<br>      &quot;name&quot;: &quot;globalFont&quot;,  //&lt;-- I create a parameter called &quot;globalFont&quot;<br>      &quot;value&quot;: &quot;segoe ui&quot;    //&lt;-- define the value for the globalFont parameter (segoe ui font name)<br>    },<br>    {<br>      &quot;name&quot;: &quot;globalFontAxis&quot;, //&lt;-- define another parameter called &quot;globalFontAxis&quot;<br>      &quot;value&quot;: &quot;segoe ui semibold&quot;  //&lt;-- define the value (segeo ui semibold font name)<br>    }<br>  ],<br>/*------------------------------------------------------------------<br>define or format individual axis objects<br>------------------------------------------------------------------*/<br>  &quot;axis&quot;: {                      //&lt;&lt;--- open global axis properties<br>    &quot;labelFont&quot;: {<br>      &quot;expr&quot;: &quot;globalFontAxis&quot;<br>    }<br>//    &quot;labelFont&quot;: &quot;segoe ui&quot;  //&lt;-- standard method for formatting options<br>    ,<br>    &quot;labelFontSize&quot;: 16,<br>    &quot;titleFont&quot;: {&quot;expr&quot;: &quot;globalFont&quot;},<br>    &quot;title&quot;: null,<br>    &quot;tickCount&quot;: 4,<br>    &quot;ticks&quot;: false,<br>    &quot;domain&quot;: false,<br>    &quot;gridDash&quot;: [2, 4],<br>    &quot;gridOpacity&quot;: 0.4<br>  },                            //&lt;&lt;--- close global axis properties<br><br>  &quot;axisX&quot;: {                    //&lt;&lt;--- open X-axis properties<br>    &quot;ticks&quot;: true,<br>    &quot;domain&quot;: true<br>  },                            //&lt;&lt;--- close X-axis properties<br><br>  &quot;axisY&quot;: {                    //&lt;&lt;--- open Y-axis properties<br>    &quot;labelPadding&quot;: 18,<br>    &quot;ticks&quot;: false,<br>    &quot;domain&quot;: false<br>  },                            //&lt;&lt;--- close Y-axis properties<br><br>  &quot;mark&quot;: {<br>    &quot;font&quot;: {&quot;expr&quot;: &quot;globalFont&quot;}<br>  }<br>}</pre><p>And in the Specification tab, we’ll copy and paste the base chart spec:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*BqZiF7NZksZABsBL2k5ZRg.png" /></figure><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;params&quot;: [],                  //&lt;-- blank params array (we&#39;ll return to ths later)<br>  &quot;transform&quot;: [],               //&lt;-- blank transforms array (more on this later also)<br>  &quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;xOffset&quot;: 0,<br>        &quot;color&quot;: &quot;#004385&quot;,      //&lt;-- colour value<br>        &quot;stroke&quot;: &quot;black&quot;,       //&lt;-- stroke value<br>        &quot;strokeWidth&quot;: 1         //&lt;-- stroke width<br>      }<br>    }<br>  ],<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;Date&quot;,<br>      &quot;type&quot;: &quot;ordinal&quot;,<br>      &quot;timeUnit&quot;: &quot;yearmonth&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;format&quot;: &quot;%b-%y&quot;<br>      }<br>    },<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;AC&quot;,<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;axis&quot;: {<br>        &quot;format&quot;: &quot;£0,,.#M&quot;,<br>        &quot;formatType&quot;: &quot;pbiFormat&quot;<br>      }<br>    }<br>  }<br>}</pre><h4>Step 2: Utilise parameter values</h4><p>Now we have a few options where we can place the parameter, either within the Spec or the Config, there’s no right or wrong… just a matter of preference. I’ll show both, the effect is exactly the same.</p><p><strong>Option 1: Spec<br></strong>First, I’ll make an intentional mistake to emphasise how things can go wrong and to highlight where we need to take extra care in coding in parameters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*BqZiF7NZksZABsBL2k5ZRg.png" /></figure><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;params&quot;: [<br>    {                          // &lt;-- add parameter between curly braces<br>      &quot;name&quot;: &quot;_barColour&quot;,        //&lt;-- parameter name<br>      &quot;value&quot;: &quot;#004385&quot;           //&lt;-- parameter value<br>    }                          // &lt;-- close braces<br>  ],<br>  &quot;transform&quot;: [],<br>  &quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;xOffset&quot;: 0,<br>   //   &quot;color&quot;: &quot;#004385&quot;      &lt;-- original code<br>        &quot;color&quot;: &quot;_barColour&quot;,  //&lt;-- reference your parameter name (p.s this isn&#39;t going to work)<br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;strokeWidth&quot;: 1<br>      }<br>    }<br>  ],<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {...},<br>    &quot;y&quot;: {...}<br>  }<br>}</pre><p>and the result…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/675/1*NpFBr4eQMkFLXJ_bVlMeQQ.png" /></figure><p>But why doesn’t this work? Let’s consider what the VL code is saying:</p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;params&quot;: [<br>    {                          // &lt;-- add parameter between curly braces<br>      &quot;name&quot;: &quot;_barColour&quot;,        //&lt;-- parameter name<br>      &quot;value&quot;: &quot;#004385&quot;           //&lt;-- parameter value<br>    }                          // &lt;-- close braces<br>  ],<br>  &quot;transform&quot;: [],<br>  &quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {...<br><br>/* -----------------------------------------------------------------------<br>• here, the color object is looking for a colour value value &quot;#004385&quot;<br>• this is a proper colour HexCode (polynesian blue) and works fine<br>----------------------------------------------------------------------- */<br><br>    &quot;color&quot;: &quot;#004385&quot;      // &lt;-- colour is looking for a value &quot;#004385&quot;<br><br>/* -----------------------------------------------------------------------<br>• here, the color object is looking for a colour value called &quot;_barColour&quot;<br>• no such colour exists in the rainbow or the list of CSS colour names<br>----------------------------------------------------------------------- */<br>    &quot;color&quot;: &quot;_barColour&quot;,  // &lt;-- colour is looking for a value &quot;_barColour&quot;<br><br>/* -----------------------------------------------------------------------<br>• here, the color object contains an expression (&quot;expr&quot;:) <br>• this enables Vega-Lite to look for a parameter called &quot;_barColour&quot;<br>• the parameter contains the colour value &quot;#004385&quot;<br>----------------------------------------------------------------------- */<br>    &quot;color&quot;:{&quot;expr&quot;: &quot;_barColour&quot; },  // &lt;-- colour is looking for a parameter &quot;_barColour&quot;<br>        <br>    ...}<br>    }<br>  ],<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {...},<br>    &quot;y&quot;: {...}<br>  }<br>}</pre><p>So when we run the amended code, we get success!</p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;params&quot;: [<br>    {<br>      &quot;name&quot;: &quot;_barColour&quot;,              //&lt;-- magic recipe<br>      &quot;value&quot;: &quot;#004385&quot;<br>    }<br>  ],<br>  &quot;transform&quot;: [],<br>  &quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;xOffset&quot;: 0,<br>        &quot;color&quot;:{&quot;expr&quot;: &quot;_barColour&quot; },  //&lt;&lt;-- magic recipe<br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;strokeWidth&quot;: 1<br>      }<br>    }<br>  ],<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;Date&quot;,<br>      &quot;type&quot;: &quot;ordinal&quot;,<br>      &quot;timeUnit&quot;: &quot;yearmonth&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;format&quot;: &quot;%b-%y&quot;<br>      }<br>    },<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;AC&quot;,<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;axis&quot;: {<br>        &quot;format&quot;: &quot;£0,,.#M&quot;,<br>        &quot;formatType&quot;: &quot;pbiFormat&quot;<br>      }<br>    }<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*6-gJXqCpXiAJRUDxo4UTCA.png" /><figcaption>Parameters in the Spec tab</figcaption></figure><p>Let’s try it again, puting the parameters in the Config tab.</p><p><strong>Option 2: Config<br></strong>Copy and paste the parameter including the curly braces and place it in the Config section, at the end of our parameters array (between the square brackets).</p><pre>// copy this bit<br>{<br>  &quot;name&quot;: &quot;_barColour&quot;,<br>  &quot;value&quot;: &quot;#004385&quot;<br>}</pre><p>and paste it here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/367/1*bGiwuFPbEnTGNL0c7ZLhUA.png" /></figure><pre>{<br>  &quot;view&quot;: {&quot;stroke&quot;: &quot;transparent&quot;},<br>  &quot;params&quot;: [ //&lt;&lt;--- open parameters array<br>    {<br>      &quot;name&quot;: &quot;globalFont&quot;,<br>      &quot;value&quot;: &quot;segoe ui&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;globalFontAxis&quot;,<br>      &quot;value&quot;: &quot;segoe ui semibold&quot;<br>    },<br>//------------------------------------------ copy and paste<br>    {<br>      &quot;name&quot;: &quot;_barColour&quot;,  //&lt;-- paste <br>      &quot;value&quot;: &quot;#004385&quot;     //&lt;-- paste<br>    }<br>//------------------------------------------ copy and paste<br>  ],          //&lt;&lt;--- close parameters array<br>  &quot;axis&quot;: {<br>    &quot;labelFont&quot;: {<br>      &quot;expr&quot;: &quot;globalFontAxis&quot;<br>    },<br>    &quot;labelFontSize&quot;: 16,<br>    &quot;titleFont&quot;: {&quot;expr&quot;: &quot;globalFont&quot;},<br>    &quot;title&quot;: null,<br>    &quot;tickCount&quot;: 4,<br>    &quot;ticks&quot;: false,<br>    &quot;domain&quot;: false,<br>    &quot;gridDash&quot;: [2, 4],<br>    &quot;gridOpacity&quot;: 0.4<br>  },<br>  &quot;axisX&quot;: {<br>    &quot;ticks&quot;: true,<br>    &quot;domain&quot;: true<br>  },<br>  &quot;axisY&quot;: {<br>    &quot;labelPadding&quot;: 18,<br>    &quot;ticks&quot;: false,<br>    &quot;domain&quot;: false<br>  },<br>  &quot;mark&quot;: {<br>    &quot;font&quot;: {&quot;expr&quot;: &quot;globalFont&quot;}<br>  }<br>}</pre><p>And run your new spec:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*BqZiF7NZksZABsBL2k5ZRg.png" /></figure><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;params&quot;: [  <br>  //--- empty / no parameters, it&#39;s living in the Config tab now ---\\<br>  ],<br>  &quot;transform&quot;: [],<br>  &quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;xOffset&quot;: 0,<br>        &quot;color&quot;:{&quot;expr&quot;: &quot;_barColour&quot; },    //&lt;-- magic recipe<br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;strokeWidth&quot;: 1<br>      }<br>    }<br>  ],<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;Date&quot;,<br>      &quot;type&quot;: &quot;ordinal&quot;,<br>      &quot;timeUnit&quot;: &quot;yearmonth&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;format&quot;: &quot;%b-%y&quot;<br>      }<br>    },<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;AC&quot;,<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;axis&quot;: {<br>        &quot;format&quot;: &quot;£0,,.#M&quot;,<br>        &quot;formatType&quot;: &quot;pbiFormat&quot;<br>      }<br>    }<br>  }<br>}</pre><p>So our Spec is less cluttered, and you’ll see this works perfectly the same.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*Rxxn8ME9NWHXlypfz-ahiw.png" /><figcaption>Parameters in the Config tab</figcaption></figure><p>Time for a break? 🥹☕</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*DDRZXHENEyhoWYDyVHT1-w.gif" /></figure><h4>Step 3: Conditional Formatting</h4><p>Welcome back 🥰… at this stage, I’d say we are pretty comfortable with how parameters? For our next challenge, we’ll tackle the juicy bit — Conditional Formatting!</p><p>Some reminders… we are using the following fields in our dataset</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/183/1*8e8Ct0T_KZ_Nvy5cP6msvw.png" /></figure><p>The field [isYTD_INT] (is the [Date] within the Year to Date, as <em>Int64.Type</em> integer) is a column added to our DIM_Calendar table, with 1s and 0s</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/843/1*9MTB8JygPsNSPvM5BITM9w.png" /></figure><p>This is nice and we’ll use this to our advantage.</p><p>First we want to create two parameters, one colour for the Actual (ACT), and another colour for the Forecast (FRC). So we’ll amend our parameters to make this distinction clear.</p><p>Head back to the Config tab and add two more parameters:</p><ul><li>_barColourACT</li><li>_barColourFRC</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*bGiwuFPbEnTGNL0c7ZLhUA.png" /></figure><pre>  &quot;params&quot;: [<br>    {<br>      &quot;name&quot;: &quot;globalFont&quot;,<br>      &quot;value&quot;: &quot;segoe ui&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;globalFontAxis&quot;,<br>      &quot;value&quot;: &quot;segoe ui semibold&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;_barColour&quot;,<br>      &quot;value&quot;: &quot;#004385&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;_barColourACT&quot;,    //&lt;-- new parameters (Actual colour)<br>      &quot;value&quot;: &quot;#004385&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;_barColourFRC&quot;,    //&lt;-- new parameters (Forecast colour)<br>      &quot;value&quot;: &quot;whitesmoke&quot;       //&lt;-- Hex code #F5F5F5<br>    }<br>  ]</pre><p>Excellent. Now stay with me, we’ll be doing a bit of back and forth comparisons, you might feel dizzy, but I want to make sure that we understand the concepts we are familiar with and how they compare in the VL context.</p><p>We are going to look at the DAX conditional formatting measure and recreate the same effect using a VL expression.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/1*ir9a041yYAA5oknA4tQTGA.png" /><figcaption>The DAX</figcaption></figure><p>In Vega-Lite, we have several options. Let’s look at them individually, them put them into context:</p><h4>Traditional IF Statement</h4><p><em>(</em><a href="https://vega.github.io/vega-lite/docs/datum.html"><em>datum </em></a><em>= single data point / data value)</em></p><pre>// traditional IF statement<br>&quot;color&quot;: {<br>    &quot;expr&quot;: &quot;if(datum.isYTD_INT == 1 , _barColourACT , _barColourFRC)&quot;<br>}<br>/* plain english: if the value in the [isYTD_INT] field equals 1<br>then return _barColourACT otherwise return _barColourFRC */</pre><h4>Ternary Operators (kinda like shortcuts)</h4><pre>// ternary operators<br>&quot;color&quot;: {<br>          &quot;expr&quot;: &quot;datum.isYTD_INT == 1 ? _barColourACT : _barColourFRC&quot;<br>        }<br>/* <br>• &quot;?&quot; = if the logical test true then return subsequent value...<br>• &quot;:&quot; = otherwise return subsequent value<br>• eg: &quot;logical test ? return value : return another Value&quot;<br><br>plain english: if the value in the [isYTD_INT] field equals 1<br>then return _barColourACT otherwise return _barColourFRC */</pre><p>We can see what dataset looks like in the Deneb Editor:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*kytAxeFVNyHXTVCjo1MmTg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*pcA67rSNR87TfUHzVlBJRw.png" /><figcaption>Actual vs Forecast formatting</figcaption></figure><p>This is amazing, and when you are able to break it down into digestible chunks 🍪, it’s much easer to …. digest 😅</p><p>But what if I don’t want to use a Power Query helper column? What if I want all my “calculated” columns to be derived from the Vega-Lite code itself? Don’t worry… I gotchu!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*IFRt-ymubJjJNjXMC9XqAg.gif" /></figure><h3>2. Conditional Formatting with in-line Vega-Lite expressions</h3><p>The next section might seem a bit daunting, but if you can create DAX measures or add Power Query columns, you can definitely create Vega-Lite expressions using <a href="https://vega.github.io/vega-lite/docs/calculate.html">calculate transforms</a> (<a href="https://vega.github.io/vega-lite/docs/transform.html">transformations</a>).</p><p>The idea of a calculate transform, is very similar to that of a calculated column, a power query column or a measure. We are deriving new data inside of the Vega-Lite dataset which we can then reference for our conditional formatting expressions.</p><p>The documentation gives us a framework to build calculate tranfroms:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/743/1*08EJEH_ueHq22kEv760-uA.png" /></figure><p>Let’s put this into practice. We want to recreate and replace the [isYTD_INT] field with a Vega-Lite [_isYTD] calculated field.</p><p><strong>Step 4: Create the calculate transforms<br></strong>First things first, we want to find out what TODAY() is, and flag a “1” for anything that is on or earlier than TODAY().</p><p>This is what TODAY() looks like in Power Query:</p><pre>// Power Query<br>Date.From( DateTime.FixedLocalNow() )</pre><p>Then to create the flag column, we complete the code:</p><pre>Table.AddColumn(<br>  #&quot;Inserted Multiplication&quot;, <br>  &quot;isYTD_INT&quot;, <br>  each <br>    let<br>      _today  = Date.From(DateTime.FixedLocalNow()), // today variable<br>      _result = if [Date] &lt;= _today then 1 else 0    // calculation<br>    in<br>      _result, <br>  Int64.Type<br>)</pre><p>In Vega-Lite we can do something very similar, first we want to add a transform array object to hold our array of calculate transforms:</p><pre>&quot;transform&quot;: [<br>  {<br>    &quot;calculate&quot;: &quot;datetime( now()) &quot;,  // transform NOW() into Date format<br>    &quot;as&quot;: &quot;_Today&quot;                     // name this field &quot;_Today&quot;<br>  },<br>  {<br>// if the [Date] field is &lt;= the field value _Today then 1 else 0<br>    &quot;calculate&quot;: &quot;datum[&#39;Date&#39;] &lt;= datum._Today ? 1 : 0 &quot;,<br>    &quot;as&quot;: &quot;_isYTD&quot;<br>  }<br>]</pre><p>And putting it into context, here is our Spec:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*BqZiF7NZksZABsBL2k5ZRg.png" /></figure><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;params&quot;: [],<br>  &quot;transform&quot;: [<br>// date today() transform<br>    {<br>      &quot;calculate&quot;: &quot;datetime( now()) &quot;,        <br>      &quot;as&quot;: &quot;_Today&quot;<br>    },<br>// isYTD transform<br>    {<br>      &quot;calculate&quot;: &quot;datum[&#39;Date&#39;] &lt;= datum._Today ? 1 : 0 &quot;,<br>      &quot;as&quot;: &quot;_isYTD&quot;<br>    }<br>  ],<br>  &quot;layer&quot;: [<br>    {<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;xOffset&quot;: 0,<br><br>/* ---------------------------------------------------------------<br>if the datum in our calculated tranform field &quot;_isYTD&quot; is 1<br>then return our _barColourACT parameter, otherwise _barColourFRC <br>----------------------------------------------------------------*/<br><br>        &quot;color&quot;: {<br>          &quot;expr&quot;: &quot;datum._isYTD == 1 ? _barColourACT : _barColourFRC&quot;<br>        },<br>//----------------------------------------------------------------<br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;strokeWidth&quot;: 1<br>      }<br>    }<br>  ],<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;Date&quot;,<br>      &quot;type&quot;: &quot;ordinal&quot;,<br>      &quot;timeUnit&quot;: &quot;yearmonth&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;format&quot;: &quot;%b-%y&quot;<br>      }<br>    },<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;AC&quot;,<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;axis&quot;: {<br>        &quot;format&quot;: &quot;£0,,.#M&quot;,<br>        &quot;formatType&quot;: &quot;pbiFormat&quot;<br>      }<br>    }<br>  }<br>}</pre><p>We can also check how these calculate transform behave within the VL dataset in the Editor’s data viewer:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/851/1*owCv5Fom92L8PtTpdfijPQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*ZZiE-Jqbv6duqi7Fxe2KDw.gif" /></figure><p>Officially MINDBLOWN 🤯🤪!<br>I’ve broken down all these steps AND MORE in the PBIX file. I had intended to write more, but I think this is a sensible place to pause for today 😅🙏🏽. So I’ll call this article Part 1. In Part 2 next time, I’ll look at more conditional formatting techniques using the <a href="https://vega.github.io/vega-lite/docs/condition.html">Condition</a> expression in the Encoding channel.</p><p>I hope you find the article useful, thank you for following me on this journey — until next time… <strong>#StayQueryous</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*URMQxNOaWlLwolWdwTjv9Q.png" /></figure><p>🔗<strong>Github link to PBIX: </strong><a href="https://github.com/PBIQueryous/Deneb/blob/main/Medium-VegaLite-Series/EP06%20Deneb%20VegaLite%20Series%20-%20Expression%20and%20Conditional%20Formatting%20pt1.pbix"><strong>EP06 Deneb VegaLite Series.pbix</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iTBuGIBNrFJjSQied664pQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eab3ca34f0de" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deneb & Vega-Lite Walkthrough Series | EP05: LAYERS ]]></title>
            <link>https://pbi-datavizzle.medium.com/deneb-vega-lite-walkthrough-series-ep05-layers-4ab67067f717?source=rss-23e0d5d396ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/4ab67067f717</guid>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[power-bi-tutorials]]></category>
            <category><![CDATA[deneb]]></category>
            <category><![CDATA[power-bi]]></category>
            <category><![CDATA[vega-lite]]></category>
            <dc:creator><![CDATA[PBI DataVizzle]]></dc:creator>
            <pubDate>Sat, 09 Mar 2024 19:48:47 GMT</pubDate>
            <atom:updated>2024-03-10T07:39:50.882Z</atom:updated>
            <content:encoded><![CDATA[<h4>Welcome to the Layer Cake🕊️ 🧙🏼‍♂ ️✨</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7oBEjpf9m0CmwyEQXp0csw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*np4uF0xj5OyC7un0SKTCMg.png" /></figure><p>💌 <em>PBIX file available at the end of the article… Enjoy!</em></p><h3>Recap</h3><p>So far in the series, we have covered Marks, Encoding, Mark Types, Mark and Mark Styling. This series on <a href="https://vega.github.io/vega-lite/docs/layer.html"><strong>LAYERS</strong></a><strong> </strong>is going to dive straight into the deep-end. If there are any unfamiliar concepts, please revisit previous episodes: <a href="https://medium.com/@pbiqueryous/power-bi-deneb-vega-lite-series-ep01-marks-and-encoding-lets-learn-to-run-before-we-fly-87f4961a2fa5">EP1</a>, <a href="https://medium.com/@pbiqueryous/deneb-vega-lite-series-ep02-mark-types-cd7a99d4a23c">EP2</a>, <a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walk-through-series-ep03-styling-mark-properties-4dddb9e0f353">EP3</a>, <a href="https://medium.com/@pbiqueryous/deneb-vega-lite-walkthrough-series-ep04-styling-mark-properties-pt2-6cafb8a2e552">EP4</a>.</p><h3>Pre-Layer (Single View)</h3><p>Before we can wrap our heads around the concept of layers in Vega-Lite (VL), we should take baby steps 🐣. Personally, I’m a visual-learner… I absorb code and code patterns by seeing them laid out in context, and being able to compare and desconstruct can be massively helpful 🤓</p><p>What do <a href="https://vega.github.io/vega-lite/docs/spec.html#single">single view</a> and <a href="https://vega.github.io/vega-lite/docs/layer.html">multi-view</a> charts look like? The VL syntax does contain important differences that we need to be aware of. Let’s <a href="https://dictionary.cambridge.org/dictionary/english/have-take-a-gander">have a gander</a>…</p><h4>Single View</h4><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;mark&quot;: {...},                    //   &lt;--- single mark<br>  &quot;encoding&quot;: {                     //   &lt;--- encoding channel<br>    &quot;x&quot;: {...},<br>    &quot;y&quot;: {...}<br>  }<br>}</pre><h4>Multi / Composite View</h4><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>/* ------------------------------------------------|<br>*nb: note the square brackers [], this is to enable<br>an array or list of multiple view specifications<br>|--------------------------------------------------*/<br><br>  &quot;layer&quot;: [          //   &lt;--- start layer property<br>    {                 //       &lt;--- start mark spec (1)<br>      &quot;mark&quot;: {...}   //         &lt;--- Mark1<br>    },                //       &lt;--- end mark spec (1)<br><br>    {                 //       &lt;--- start mark spec (2)<br>      &quot;mark&quot;: {...}   //         &lt;--- Mark2<br>    }                 //       &lt;--- end mark spec (2)<br>  ],                  //   &lt;--- end layer property<br><br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {...},<br>    &quot;y&quot;: {...}<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*CNzrtYK_GPrLc7UEfEku8g.png" /><figcaption>Single vs Multi Layered View</figcaption></figure><p>When we obvserve the code this way, it’s not as complicated as we might first have imagined. It’s a case of putting our <em>{ marks }</em> inside the <em>layer [ {mark1}, {mark2} ]</em></p><h3>Multi-Layer Charts</h3><p>So far so good? Ready for the good stuff? Me too 😏 — Without further a-do, let us build!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*2poC9qbf3DwYWg9v3cvnuw.gif" /></figure><h4>Step 1: First Layer (Actual Bar)</h4><p>We’ll start with just the basics and gradually increase the complexity. I’ve prepared some visuals in the <a href="https://github.com/PBIQueryous/Deneb/blob/main/Medium-VegaLite-Series/EP05%20Deneb%20VegaLite%20Series%20-%20Layers.pbix">PBIX</a> so we can hit the ground running… 🏃🏽</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*7IJjhZDJnA8QVARdxMJtwQ.png" /></figure><p><strong>Config:</strong></p><p>Use this script for the config, by placing it here, we keep our specification code less cluttered, and easier to read 😊. I’m basically setting the fonts to “Segoe UI” and the font size for the labels to 16.</p><pre>{<br>  &quot;view&quot;: {&quot;stroke&quot;: &quot;transparent&quot;},<br>  &quot;params&quot;: [                      //    &lt;--- declare some parameters<br>    {<br>      &quot;name&quot;: &quot;globalFont&quot;,        // &lt;-- give parameter a name<br>      &quot;value&quot;: &quot;segoe ui&quot;          // &lt;-- parameter value<br>    },<br>    {<br>      &quot;name&quot;: &quot;globalFontAxis&quot;,    // &lt;-- another parameter name<br>      &quot;value&quot;: &quot;segoe ui semibold&quot; // &lt;-- parameter value<br>    }<br>  ],                              //    &lt;--- end parameters property<br>  &quot;axis&quot;: {                      // applies config to all axis properties in the specification<br>    &quot;labelFont&quot;: {<br>      &quot;expr&quot;: &quot;globalFontAxis&quot;<br>    },<br>    &quot;labelFontSize&quot;: 16,<br>    &quot;titleFont&quot;: {&quot;expr&quot;: &quot;globalFont&quot;}  // &lt;-- expression to return paramter value<br>  },<br>  &quot;legend&quot;: {<br>    &quot;labelFont&quot;: {&quot;expr&quot;: &quot;globalFont&quot;}, // &lt;-- expression to return paramter value<br>    &quot;titleFont&quot;: {&quot;expr&quot;: &quot;globalFont&quot;}<br>  },<br>  &quot;header&quot;: {<br>    &quot;labelFont&quot;: {&quot;expr&quot;: &quot;globalFont&quot;},<br>    &quot;titleFont&quot;: {&quot;expr&quot;: &quot;globalFont&quot;}<br>  },<br>  &quot;mark&quot;: {<br>    &quot;font&quot;: {&quot;expr&quot;: &quot;globalFont&quot;}<br>  },<br>  &quot;title&quot;: {<br>    &quot;font&quot;: {&quot;expr&quot;: &quot;globalFont&quot;},<br>    &quot;subtitleFont&quot;: {<br>      &quot;expr&quot;: &quot;globalFont&quot;<br>    }<br>  }<br>}</pre><p><strong>Specification:</strong></p><p>This will give us a nicely formatted set of axes and a tidy bar chart. I’ve used <a href="https://vega.github.io/vega-lite/docs/scale.html#band"><em>band </em></a>scaling, to provide a narrow bar shape and even spacing to provide some room for our layers.</p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;layer&quot;: [                   // &lt;&lt;--- open layer<br>    {                            // &lt;-- open mark1<br>      &quot;mark&quot;: {                    <br>        &quot;type&quot;: &quot;bar&quot;,             // &lt;-- mark type<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},   // &lt;-- width of bars <br>        &quot;fill&quot;: &quot;#004385&quot;,         // &lt;-- bar colour<br>        &quot;stroke&quot;: &quot;black&quot;,         // &lt;-- bar border colour<br>        &quot;xOffset&quot;: 0               // &lt;-- bar offset (more on this later)<br>      }<br>    }                           // &lt;-- close mark1<br>  ],                           // &lt;&lt;--- close layer<br><br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {                        <br>      &quot;field&quot;: &quot;Date&quot;,<br>      &quot;type&quot;: &quot;ordinal&quot;,<br>      &quot;timeUnit&quot;: &quot;yearmonth&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;format&quot;: &quot;%b-%y&quot;<br>      },<br>      &quot;scale&quot;: {&quot;padding&quot;: 0.3}<br>    },<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;AC&quot;,            // &lt;-- actual sales field (AC)<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;tickCount&quot;: 4,<br>        &quot;format&quot;: &quot;£0,,.#M&quot;,<br>        &quot;formatType&quot;: &quot;pbiFormat&quot;,<br>        &quot;labelPadding&quot;: 18<br>      }<br>    }<br>  }<br>}</pre><p><strong>Step 2: Second Layer (Budget Bar)</strong></p><p>For illustrative purposes, I want us to build a separate chart so we can better conceptualise the notion of <em>layers.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/943/1*ylAjkztFPZdROdRFxnmzZA.png" /></figure><p>We are super efficient, so we will simply copy and paste the entire visual and then tweek a couple of lines of code:</p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;layer&quot;: [                   <br>    {                            <br>      &quot;mark&quot;: {                    <br>        &quot;type&quot;: &quot;bar&quot;,             <br>        &quot;width&quot;: {&quot;band&quot;: 0.65},   <br>        &quot;fill&quot;: &quot;#FDFFFC&quot;,         // &lt;-- chnage the bar colour<br>        &quot;stroke&quot;: &quot;black&quot;,         <br>        &quot;xOffset&quot;: 0               <br>      }<br>    }                           <br>  ],                           <br><br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {                        <br>      &quot;field&quot;: &quot;Date&quot;,<br>      &quot;type&quot;: &quot;ordinal&quot;,<br>      &quot;timeUnit&quot;: &quot;yearmonth&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;format&quot;: &quot;%b-%y&quot;<br>      },<br>      &quot;scale&quot;: {&quot;padding&quot;: 0.3}<br>    },<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;BU&quot;,            // &lt;-- change to budget sales field (BU)<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;tickCount&quot;: 4,<br>        &quot;format&quot;: &quot;£0,,.#M&quot;,<br>        &quot;formatType&quot;: &quot;pbiFormat&quot;,<br>        &quot;labelPadding&quot;: 18<br>      }<br>    }<br>  }<br>}</pre><p>Great progress! This is an ideal moment to take a step back, grab a coffee ☕, and enjoy the fruits of our labour:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yiMMpr_-bzmNVtYIBexqlg.png" /></figure><p><strong>Step 3: Combining these layers</strong></p><p>At first glance, the idea of layering is quite straight forward. A fellow data viz wizard and Deneb (VL) artist <a href="https://thysvdw.github.io/">Thys</a> said it best…</p><blockquote>It’s like printing charts on paper, and placing them on top of each other…</blockquote><p>This is wonderful in it’s simplicity and when things start to get complicated or overwhelming with VL, I always give myself a shake and come back to this description.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cIBjhHy3em39YH6x.jpg" /><figcaption>Layers</figcaption></figure><p>We are powering up! Now we will combine our two layers into a single chart. All we need to is to duplicate (copy and paste) our mark properties into the existing layer, and amend a few properties so we end up with two marks, one for Actual and the other for Budget:</p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;layer&quot;: [                    //--- open layer<br>//------- COPY AND PASTE THIS SECTION<br>    {              <br>      &quot;mark&quot;: {                  // &lt;-- actual bar mark<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;fill&quot;: &quot;#004385&quot;,       // &lt;-- actual bar colour <br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;xOffset&quot;: 0             // &lt;-- actual bar offset is ZERO<br>      }<br>    }<br>//------- COPY AND PASTE THIS SECTION and paste below<br>//------- SEPARATED BY A COMMA<br>,<br>    {                                  <br>      &quot;mark&quot;: {                  // &lt;-- budget bar mark<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;fill&quot;: &quot;#FDFFFC&quot;,       // &lt;-- budget bar colour<br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;xOffset&quot;: 0             // &lt;-- budget bar offset is ZERO (keep note)         <br>      }<br>    }<br>  ],                            //--- close layer<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;Date&quot;,<br>      &quot;type&quot;: &quot;ordinal&quot;,<br>      &quot;timeUnit&quot;: &quot;yearmonth&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;format&quot;: &quot;%b-%y&quot;<br>      },<br>      &quot;scale&quot;: {&quot;padding&quot;: 0.3}<br>    },<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;AC&quot;,<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;tickCount&quot;: 4,<br>        &quot;format&quot;: &quot;£0,,.#M&quot;,<br>        &quot;formatType&quot;: &quot;pbiFormat&quot;,<br>        &quot;labelPadding&quot;: 18<br>      }<br>    }<br>  }<br>}</pre><p>Hit play and take a bow you geni…… oh wait 😐</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-L8V54TxVZDw8RhoqQvRdw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*t3UEnQR8ECKK-p6V1pAlVw.gif" /></figure><p><strong>Step 4: Cry</strong> 😭<br>Erm. This is awkward. But we don’t have time to cry into our cornflakes just yet. We can fix this. First things first. We need to shift the bars along the X-Axis to create some separation…remember the “xOffset” attribute? Let’s head back to the code:</p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;layer&quot;: [                   <br><br>    {              <br>      &quot;mark&quot;: {                  <br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;fill&quot;: &quot;#004385&quot;,       <br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;xOffset&quot;: 0        // &lt;-- actual bar offset is ZERO<br>      }<br>    }<br><br>,<br>    {                                  <br>      &quot;mark&quot;: {                  <br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;fill&quot;: &quot;#FDFFFC&quot;,       <br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;xOffset&quot;: -8       // &lt;-- set the budget bar offset to -8 (8 pixels to the left)<br>      }<br>    }<br>  ],                            <br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {...},<br>    &quot;y&quot;: {...}<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H70cIl8P5WTKorF5a5A4rA.png" /></figure><p>By setting the xOffset to -8, we have shifted the Budget mark 8 pixels to the left along the X-axis. Winning!</p><p>Now we can see both bars… but there’s still issues.</p><ol><li>The Budget mark is returning the same values as the Actual mark</li><li>The Actual bar is behind the Budget mark, we want the flip the order around so the Actual bar is in front</li></ol><p>So, we have our 2 marks, the Actual bar mark is encoding as intended, but the Budget mark isn’t playing ball. You’ll be pleased to know at this juncture we will be taking a trip into the 4th dimension… #SorryNotSorry</p><h4>Step 5a: Understanding Encoding Behaviour</h4><p>We want to make sure the Budget mark is encoded against the Budget field in our dataset. Let’s compare several variations of how we can write the code to better understand who encoding behaves across multiple marks.</p><p>I’ve included a few in-code descriptors to ease the congitive burden. Firstly, let’s look at how the position of the <em>encoding </em>channel can effect the viz:</p><p><strong>Example A: encoding channel <em>below </em>layer</strong></p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;layer&quot;: [<br>    {<br>      &quot;name&quot;: &quot;ACTUAL BAR&quot;,      // &lt;-- define mark name (ACTUAL BAR)<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;fill&quot;: &quot;#004385&quot;,<br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;xOffset&quot;: 0<br>      }<br>    },<br>    {<br>      &quot;name&quot;: &quot;BUDGET BAR&quot;,    // &lt;-- define mark name (BUDGET BAR)<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;fill&quot;: &quot;#FDFFFC&quot;,<br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;xOffset&quot;: -8<br>      }<br>    }<br>  ],<br>/*---------------------------------------|<br>ENCODING CHANNEL PLACED BELOW LAYER MARKS<br>|---------------------------------------*/<br>  &quot;name&quot;: &quot;GLOBAL ENCODING&quot;,  // &lt;-- coding channel affects entire layer<br>  &quot;description&quot;: &quot;applies encoding to the entire layer above&quot;,<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;Date&quot;,<br>      &quot;type&quot;: &quot;ordinal&quot;,<br>      &quot;timeUnit&quot;: &quot;yearmonth&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;format&quot;: &quot;%b-%y&quot;<br>      },<br>      &quot;scale&quot;: {&quot;padding&quot;: 0.3}<br>    },<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;AC&quot;,<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;tickCount&quot;: 4,<br>        &quot;format&quot;: &quot;£0,,.#M&quot;,<br>        &quot;formatType&quot;: &quot;pbiFormat&quot;,<br>        &quot;labelPadding&quot;: 18<br>      }<br>    }<br>  }<br>}</pre><p><strong>Example B: encoding channel <em>above </em>layer</strong></p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>/*---------------------------------------|<br>ENCODING CHANNEL PLACED ABOVE LAYER MARKS<br>|---------------------------------------*/<br>  &quot;name&quot;: &quot;GLOBAL ENCODING&quot;,  // &lt;-- coding channel affects entire layer<br>  &quot;description&quot;: &quot;applies encoding to the entire layer below&quot;,<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;Date&quot;,          // &lt;-- X-axis encodes the Date field<br>      &quot;type&quot;: &quot;ordinal&quot;,<br>      &quot;timeUnit&quot;: &quot;yearmonth&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;format&quot;: &quot;%b-%y&quot;<br>      },<br>      &quot;scale&quot;: {&quot;padding&quot;: 0.3}<br>    },<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;AC&quot;,            // &lt;-- Y-axis encodes the AC field!!<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;tickCount&quot;: 4,<br>        &quot;format&quot;: &quot;£0,,.#M&quot;,<br>        &quot;formatType&quot;: &quot;pbiFormat&quot;,<br>        &quot;labelPadding&quot;: 18<br>      }<br>    }<br>  }<br>  &quot;layer&quot;: [<br>    {<br>      &quot;name&quot;: &quot;ACTUAL BAR&quot;,      // &lt;-- define mark name (ACTUAL BAR)<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;fill&quot;: &quot;#004385&quot;,<br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;xOffset&quot;: 0<br>      }<br>    },<br>    {<br>      &quot;name&quot;: &quot;BUDGET BAR&quot;,    // &lt;-- define mark name (BUDGET BAR)<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;fill&quot;: &quot;#FDFFFC&quot;,<br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;xOffset&quot;: -8<br>      }<br>    }<br>  ]<br>}</pre><p><strong>In Brief:</strong> The position of the “global” encoding channel doesn’t effect the chart. But it’s clear now that the reason why the Budget bar isn’t encoding properly is because our “global” encoding is currently only bound to the AC field.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KbHi4AqQRLEpQsgyjeD7dg.png" /></figure><h4>Step 5c: Add Budget Encoding</h4><p>The quickest way to fix this is the trusty Copy and Paste. Copy the entire encoding section, and we are going to paste it inside the Budget Mark property. Change the Y-axis encoding to “BU”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/523/1*Nb_11kKca62cjqVMAXimYA.png" /></figure><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;name&quot;: &quot;GLOBAL ENCODING&quot;,<br>  &quot;description&quot;: &quot;applies encoding to the entire layer above&quot;,<br>  &quot;encoding&quot;: {                  // &lt;-- global encoding<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;Date&quot;,          // &lt;-- Date field on X-axis (globally)<br>      &quot;type&quot;: &quot;ordinal&quot;,<br>      &quot;timeUnit&quot;: &quot;yearmonth&quot;,<br>      &quot;axis&quot;: {...}<br>    },<br>    &quot;y&quot;: {                      // &lt;-- AC field on Y-axis (globally)<br>      &quot;field&quot;: &quot;AC&quot;,<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;axis&quot;: {...}<br>    }<br>  },<br>  &quot;layer&quot;: [<br>    {<br>      &quot;name&quot;: &quot;ACTUAL BAR&quot;,     // &lt;-- actual bar mark<br>      &quot;mark&quot;: {...}<br>    },                          // &lt;-- no encoding on this mark  <br>                                // so it applies the global encoding<br>    {<br>      &quot;name&quot;: &quot;BUDGET BAR&quot;,     // &lt;-- budget bar mark<br>      &quot;mark&quot;: {...},<br>      &quot;encoding&quot;: {             // &lt;-- mark-specific encoding<br>        &quot;x&quot;: {<br>           &quot;field&quot;: &quot;Date&quot;,     // &lt;-- same Date field<br>           ...},             <br>        &quot;y&quot;: {<br>          &quot;field&quot;: &quot;BU&quot;,        // &lt;-- new budget encoding overwrites  <br>                                // the global encoding<br>          &quot;type&quot;: &quot;quantitative&quot;,<br>          &quot;axis&quot;: {...}<br>        }<br>      }<br>    }<br>  ]<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cQQoEmC_jHPu462JANfgtA.png" /></figure><p>Magic! Before we move on, whilst we are still in the headspace of Encoding. We must MUST talk about efficent and succinct coding.. after all, coding is just a love letter to your future self 💌🤓</p><p>You’ll have noticed that in developing a lovely Deneb / Vega-Lite viz, the number of lines of code grows rather quickly. To ensure our code is as (obsessively) neat and tidy as possible, we want to reduce any unnecessary duplication or redundant code.</p><p>As we have seen, we have a global encoding channel, and any subsequent mark-specific encoding properties we add will in fact overwrite the global properties. This is perfect. We don’t actually have to copy every line of the encoding channel for this to work. We actually only need to add the parts we want to change. I’m probably not making much sense now, let me show you:</p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;encoding&quot;: {                  // &lt;-- global encoding<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;Date&quot;,<br>      &quot;type&quot;: &quot;ordinal&quot;,<br>      &quot;timeUnit&quot;: &quot;yearmonth&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;format&quot;: &quot;%b-%y&quot;<br>      },<br>      &quot;scale&quot;: {&quot;padding&quot;: 0.3}<br>    },<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;AC&quot;,            // &lt;-- this is only part of the encoding<br>                                // we will be changing below...<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;axis&quot;: {<br>        &quot;title&quot;: null,<br>        &quot;tickCount&quot;: 4,<br>        &quot;format&quot;: &quot;£0,,.#M&quot;,<br>        &quot;formatType&quot;: &quot;pbiFormat&quot;,<br>        &quot;labelPadding&quot;: 18<br>      }<br>    }<br>  },<br>  &quot;layer&quot;: [      // &lt;&lt;--- open layer<br>    {                          // &lt;-- start mark1 object<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;fill&quot;: &quot;#004385&quot;,<br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;xOffset&quot;: 0<br>      }<br>    },                         // &lt;-- end mark1 object<br>    {                          // &lt;-- start mark2 object<br>      &quot;mark&quot;: {<br>        &quot;type&quot;: &quot;bar&quot;,<br>        &quot;width&quot;: {&quot;band&quot;: 0.65},<br>        &quot;fill&quot;: &quot;#FDFFFC&quot;,<br>        &quot;stroke&quot;: &quot;black&quot;,<br>        &quot;xOffset&quot;: -8<br>      },<br>      &quot;encoding&quot;: {              // &lt;-- mark-specific encoding<br>        &quot;y&quot;: {&quot;field&quot;: &quot;BU&quot;}     // &lt;-- only changed the &quot;field&quot; property<br>                                 // all the other global encoding properties<br>                                 // will still apply<br>      }<br>    }                          // &lt;-- end mark2 object<br>  ]              // &lt;&lt;--- close layer<br>}</pre><p>And the result is the same, but our code is cleaner, and we feel better for it… don’t we? 😊</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*0Jt9U1tiPksOS-xl4sU4xA.gif" /></figure><p>For the penultimate step, to reinforce the flexibility and variety of encoding options, I’ll share abbreviated version so you can compare the difference.</p><p><strong>Example C: Partial Global / Defined at the Mark-level</strong></p><pre>/* NB: ABBREVIATED FOR ILLUSTRATIVE PURPOSES */<br>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {                               // &lt;-- global x-axis<br>      &quot;field&quot;: &quot;Date&quot;<br>    },<br>    &quot;y&quot;: {<br>                                         // &lt;-- removed global y-axis field<br>      &quot;type&quot;: &quot;quantitative&quot;,            // &lt;-- partial y-axis encoding<br>      &quot;axis&quot;: {...}<br>    }<br>  },<br>  &quot;layer&quot;: [<br>    {<br>      &quot;name&quot;: &quot;ACTUAL BAR&quot;,<br>      &quot;mark&quot;: {...},<br>      &quot;encoding&quot;: {&quot;y&quot;: {&quot;field&quot;: &quot;AC&quot;}}  // &lt;-- mark-specific y-axis<br>    },<br>    {<br>      &quot;name&quot;: &quot;BUDGET BAR&quot;,<br>      &quot;mark&quot;: {...},<br>      &quot;encoding&quot;: {&quot;y&quot;: {&quot;field&quot;: &quot;BU&quot;}} // &lt;-- mark specific y-axis<br>    }<br>  ]<br>}</pre><p>Example D: <strong>Partial Global / Defined at the Mark-level</strong></p><pre>/* NB: ABBREVIATED FOR ILLUSTRATIVE PURPOSES */<br>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br> <br>  &quot;layer&quot;: [<br>    {<br>     /* place the encoding position above the mark */<br>      &quot;encoding&quot;: {&quot;y&quot;: {&quot;field&quot;: &quot;AC&quot;}}  // &lt;-- mark-specific y-axis,<br>      &quot;name&quot;: &quot;ACTUAL BAR&quot;,<br>      &quot;mark&quot;: {...}<br>      <br>    },<br>    {<br>     /* place the encoding position above the mark */<br>      &quot;encoding&quot;: {&quot;y&quot;: {&quot;field&quot;: &quot;BU&quot;}} // &lt;-- mark specific y-axis<br>      &quot;name&quot;: &quot;BUDGET BAR&quot;,<br>      &quot;mark&quot;: {...}<br>      <br>    }<br>  ],<br>/* place the global encoding channel below the the layer property */<br>&quot;encoding&quot;: {<br>    &quot;x&quot;: {                               // &lt;-- global x-axis<br>      &quot;field&quot;: &quot;Date&quot;<br>    },<br>    &quot;y&quot;: {<br>      &quot;type&quot;: &quot;quantitative&quot;,            // &lt;-- partial y-axis encoding<br>      &quot;axis&quot;: {...}<br>    }<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/1*hNhAPb6jF6DwyYzhtotLVw.png" /><figcaption>Actual bar behind the Budget bar</figcaption></figure><p>If you think that was intense. You’re are 100% correct! 🤪🤕🤭 — but there’s no more of that today… pinky swear 🤞🏽.</p><h4>Step 6: Change order of marks</h4><p>This part is a breeze — trust me. In short, the order in which you code your marks, determines the position it appears on the canvas:</p><p><strong>Example E: Actual behind the Budget</strong></p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;description&quot;: &quot;MAIN LAYER&quot;,<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {...},<br>    &quot;y&quot;: {...}<br>  },<br>  &quot;layer&quot;: [<br>    {                              // &lt;--- actual mark layered first (back)<br>      &quot;name&quot;: &quot;ACTUAL BAR&quot;,<br>      &quot;mark&quot;: {...},<br>      &quot;description&quot;: &quot;ACTUAL BAR ENCODING&quot;,<br>      &quot;encoding&quot;: {&quot;y&quot;: {&quot;field&quot;: &quot;AC&quot;}}<br>    },<br>    {<br>      &quot;name&quot;: &quot;BUDGET BAR&quot;,      // &lt;--- budget mark layered second (front)<br>      &quot;mark&quot;: {...},<br>      &quot;description&quot;: &quot;BUDGET BAR ENCODING&quot;,<br>      &quot;encoding&quot;: {&quot;y&quot;: {&quot;field&quot;: &quot;BU&quot;}}<br>    }<br>  ]<br>}</pre><p>So let’s do the switcheroo!</p><p><strong>Example F: Actual in front of Budget</strong></p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;description&quot;: &quot;MAIN LAYER&quot;,<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {...},<br>    &quot;y&quot;: {...}<br>  },<br>  &quot;layer&quot;: [<br>    {<br>      &quot;name&quot;: &quot;BUDGET BAR&quot;,      // &lt;--- budget mark layered first (back)<br>      &quot;mark&quot;: {...},<br>      &quot;description&quot;: &quot;BUDGET BAR ENCODING&quot;,<br>      &quot;encoding&quot;: {&quot;y&quot;: {&quot;field&quot;: &quot;BU&quot;}}<br>    },<br>    {                              // &lt;--- actual mark layered second (front)<br>      &quot;name&quot;: &quot;ACTUAL BAR&quot;,<br>      &quot;mark&quot;: {...},<br>      &quot;description&quot;: &quot;ACTUAL BAR ENCODING&quot;,<br>      &quot;encoding&quot;: {&quot;y&quot;: {&quot;field&quot;: &quot;AC&quot;}}<br>    }<br>  ]<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IJW6WxqYHztrI6HOOXyGdQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*kZPvzga8ORZSe1KUlDDTOg.gif" /></figure><p>I hope you survived until the end… and I hope it gives you some inspiration to create your own magic! 🧙🏽‍♂️🪄</p><p>Until next time… #StayQueryous</p><p>🔗<strong>Github link to PBIX: </strong><a href="https://github.com/PBIQueryous/Deneb/blob/main/Medium-VegaLite-Series/EP05%20Deneb%20VegaLite%20Series%20-%20Layers.pbix"><strong>EP05 — Deneb &amp; Vega-Lite Layers.pbix</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*URMQxNOaWlLwolWdwTjv9Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/829/1*sEo470eVwi9FI9O8eVgv_A.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4ab67067f717" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deneb & Vega-Lite Walkthrough Series | EP04: STYLING MARK PROPERTIES (pt2)]]></title>
            <link>https://pbi-datavizzle.medium.com/deneb-vega-lite-walkthrough-series-ep04-styling-mark-properties-pt2-6cafb8a2e552?source=rss-23e0d5d396ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/6cafb8a2e552</guid>
            <category><![CDATA[deneb]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[vega-lite]]></category>
            <category><![CDATA[power-bi-step-by-step]]></category>
            <category><![CDATA[power-bi]]></category>
            <dc:creator><![CDATA[PBI DataVizzle]]></dc:creator>
            <pubDate>Sun, 18 Feb 2024 11:09:35 GMT</pubDate>
            <atom:updated>2024-02-18T17:30:06.712Z</atom:updated>
            <content:encoded><![CDATA[<h4>Styling mark properties is a wide-ranging topic, so much so, it’s necessary to take another little dip into chart styling and customisation🕊️ 🧙🏼‍♂️✨</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CVspQDZANuDYRiJp8kmfpQ.png" /></figure><p>💌 <em>PBIX file available at the end of the article… Enjoy!</em></p><h3><strong>Recap</strong></h3><p>In the previous episode, we looked at formatting the mark properties of our column chart. In this episode, we will reuse that column chart and modify it slightly to create an area chart with custom styling.</p><h3><strong>Not Lazy, Just Efficient</strong></h3><p>We could start from scratch, but that’s unnecessary when we can make the most of the code we’ve already built.</p><blockquote>The greatest invention of all time is <strong>copy and paste — </strong>Alberto Ferrari (SQLBI)</blockquote><p>There’s no shame in copy and paste, we are busy people and we are in a hurry to showcase our talents, remember…</p><blockquote>I’m not lazy, just efficient — Patrick LeBlank (Guys In A Cube)</blockquote><h3>Walkthrough: Customised Area Chart</h3><p><strong>Step 1: Prepare the Area Chart</strong><br>In the PBIX solution file, we can first copy and paste or duplicate the column chart viz. Then return to the Editor:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_X2QeFDPcEB0oPJVeWR7hQ.png" /></figure><p><strong>Step 2: Change the mark type</strong><br>We want to start working on an area chart, so the next simple step is to replace mark type bar with area:</p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;mark&quot;: {<br>    &quot;type&quot;: &quot;area&quot;,            // &lt;-- replace &quot;bar&quot; with &quot;area&quot;<br>    &quot;color&quot;: &quot;yellow&quot;,<br>    &quot;fill&quot;: &quot;#F0E199&quot;,<br>    &quot;stroke&quot;: &quot;#E044A7&quot;,<br>    &quot;strokeWidth&quot;: 2<br>  },<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {...},<br>    &quot;y&quot;: {...}<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/809/1*WqvMwszvkdWI5QFr5xU-Ew.png" /></figure><p><strong>Step 3: Replace X-Axis</strong><br>Technically speaking, line and area charts are best suited for trend analyses, that is to say, viewing data in a continuous fashion horizontally over time. You’ll need to drop the DIM_Dates[EndOfMonth] field into the values field well, and remove [Category]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/262/1*EKhRuruWEGlfBR4twZxIqg.png" /></figure><p>You might see some strange behaviours happening to your Deneb viz, fear not… this is temporary. You need to remove the [Category] field.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*KsweOaaec3mcNSff7tksyQ.png" /></figure><p>When you remove the [Category] field, Deneb will ask you to remap the missing field. In the dropdown selection, select [EndOfMonth] and hit “Apply Mapping”. Deneb will then apply this field to the Vega-Lite specification:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xa08rVEGrp5x3plbZ9pV2w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*yJ1_03TpoCOELYXbxBC8aQ.png" /></figure><p><strong>Step 4: Fixing Sort, Data Types, and Axis Labels</strong><br>You will have noticed immediately that our Area Chart needs some TLC. In a few simple steps, we can get the chart in working order. We need to do a couple of things first:</p><ul><li>Fix the sort order</li><li>Fix the axis labels</li><li>Fix axis data types</li></ul><p>This bit is a doddle, we’ll just delete a few pieces of code…</p><pre>//** nb: comments and annotations added for effect **\\<br>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;mark&quot;: {<br>    &quot;type&quot;: &quot;area&quot;,<br>    &quot;color&quot;: &quot;yellow&quot;,<br>    &quot;fill&quot;: &quot;#F0E199&quot;,<br>    &quot;stroke&quot;: &quot;#E044A7&quot;,<br>    &quot;strokeWidth&quot;: 2<br>  },<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;EndOfMonth&quot;,<br>      &quot;type&quot;: &quot;nominal&quot;,<br>      &quot;sort&quot;: &quot;-y&quot;,          // &lt;-- delete this line here (Ctrl+D shortcut)<br>      &quot;axis&quot;: {}<br>/*     &quot;axis&quot;: {<br>        &quot;labelAngle&quot;: 325,  // &lt;-- delete this section also<br>        &quot;labelPadding&quot;: 4   // &lt;-- and delete this section<br>*/      }<br>    },<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;_AC&quot;,<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;axis&quot;: {<br>        &quot;format&quot;: &quot;£0,,.#M&quot;,<br>        &quot;formatType&quot;: &quot;pbiFormat&quot;<br>      }<br>    }<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*zXxa8f4jkui55T09qVBMRg.png" /></figure><p>Now for the Dates field. Vega-Lite requires us to be very specific about data types in general, but even more-so with date fields. They need extra attention when it comes to formatting. Let’s take a closer look:</p><p>We’ve mentioned in previous episodes that Vega-Lite needs a helping hand in defining the encoding <a href="https://vega.github.io/vega-lite/docs/type.html"><em>data type</em></a> to avoid ambiguity. We have <strong>nominal </strong>(text or categorical), <strong>ordinal </strong>(ranked), <strong>quantitative </strong>(numerical), and <strong>temporal </strong>(datetime):</p><pre>//** nb: comments and annotations added for effect **\\<br>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;mark&quot;: {<br>    &quot;type&quot;: &quot;area&quot;,<br>    &quot;color&quot;: &quot;yellow&quot;,<br>    &quot;fill&quot;: &quot;#F0E199&quot;,<br>    &quot;stroke&quot;: &quot;#E044A7&quot;,<br>    &quot;strokeWidth&quot;: 2<br>  },<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;EndOfMonth&quot;,      // &lt;-- dataset date field<br>      &quot;type&quot;: &quot;temporal&quot;,         // &lt;-- define type as &quot;temporal&quot;<br>      &quot;axis&quot;: {}<br>    },<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;_AC&quot;,<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;axis&quot;: {<br>        &quot;format&quot;: &quot;£0,,.#M&quot;,<br>        &quot;formatType&quot;: &quot;pbiFormat&quot;<br>      }<br>    }<br>  }<br>}</pre><p>And the result is very tidy. Take note, when using a <em>temporal </em>data type, Vega-Lite automatically treat it as a <em>continuous </em>field:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*raB3W7caGSTLtUGyEsbEYQ.png" /></figure><p>We can compare this with Power BI’s axis formatting for categorical and continuous axis:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TLIRcQNLgI5pMVXGFhmoSg.png" /></figure><p>Back to Deneb, our area chart is looking really good. But there is some fine tuning I’d like to apply to the axis labels. In this moment, we will be venturing into some uncharted territory. Vega-Lite provides us will near complete control of every element of the chart, whereas in Power BI we do not always have the same degree of flexibility. For example, in Power BI with a continuous axis, you do not have any options or settings to format how the axis labels appear. In fact, PBI’s continous axis changes dynamically depending on the size of the viz container, but still there is no real control:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F9D8hMKByURKCXyouKg02Q.png" /></figure><p>This is where Vega-Lite shines, and this is where we will shine too 🪄✨… ready?</p><p>We want to change how the date appears on our axis, I want to keep it simple — I only want to see the month and year. We can achieve this really cleanly by adding a <a href="https://vega.github.io/vega-lite/docs/timeunit.html"><strong><em>timeUnit</em></strong></a><strong><em> </em></strong>attribute, and 1 single line of code:</p><pre>//** nb: comments and annotations added for effect **\\<br>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;mark&quot;: {...},<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;EndOfMonth&quot;,      <br>      &quot;type&quot;: &quot;temporal&quot;,         <br>      &quot;timeUnit&quot;: &quot;yearmonth&quot;,    // &lt;-- assign a month &amp; year format<br>      &quot;axis&quot;: {}<br>    },<br>    &quot;y&quot;: {...}<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*kWuYA5ovQBa0UuIb2A6Liw.png" /></figure><p>The result is good, but I want further customisation. I’d like to apply a <strong>“mmm-yy”</strong> format, eg: Jan-23, Jul-23 etc.</p><p>To achieve this, we need to take a dive into a world of documentation pain 🤓… in the form of <a href="https://d3js.org/d3-time-format">D3’s number/date formatting</a>. I shall list a few important elements below:</p><ul><li>%b = abbreviated month name</li><li>%B = full month name</li><li>%y = 2-digit year</li><li>%Y = 4-digit year</li></ul><p>We will add a <em>format </em>attribute to the <em>axis </em>properties:</p><pre>//** nb: comments and annotations added for effect **\\<br>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;mark&quot;: {...},<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;EndOfMonth&quot;,      <br>      &quot;type&quot;: &quot;temporal&quot;,         <br>      &quot;timeUnit&quot;: &quot;yearmonth&quot;,    <br>      &quot;axis&quot;: {<br>        &quot;format&quot;: &quot;%b-%y&quot;      // &lt;-- D3 formatting &quot;mmm-yy&quot;<br>      }<br>    },<br>    &quot;y&quot;: {...}<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U4-jzsSlKYrzcQ5VeAVWzw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*6MCviLFHpcHQJl1lIaImAQ.gif" /></figure><p>Fantastic. But don’t celebrate just yet 🥳 — there is still work to do, trust me, you going to love it… just some final flourishes to make our chart immaculate 🧙‍♂️</p><p><strong>Step 5: Adjust Axis Padding</strong><br>We want to create some space between the axis labels and the chart canvas. This <strong>space </strong>is referred to as <em>padding </em>or <em>labelPadding</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FNhsWpoUw6EwtHgM7Caguw.png" /></figure><p>Returning to our code, we want to add padding to boy x and y-axis separately:</p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;mark&quot;: {<br>    &quot;type&quot;: &quot;area&quot;,<br>    &quot;color&quot;: &quot;yellow&quot;,<br>    &quot;fill&quot;: &quot;#F0E199&quot;,<br>    &quot;stroke&quot;: &quot;#E044A7&quot;,<br>    &quot;strokeWidth&quot;: 2<br>  },<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;EndOfMonth&quot;,<br>      &quot;type&quot;: &quot;temporal&quot;,<br>      &quot;timeUnit&quot;: &quot;yearmonth&quot;,<br>      &quot;axis&quot;: {<br>        &quot;format&quot;: &quot;%b-%y&quot;,<br>        &quot;labelPadding&quot;: 8      // &lt;-- 8px padding between x-axis and chart<br>      }<br>    },<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;_AC&quot;,<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;axis&quot;: {<br>        &quot;format&quot;: &quot;£0,,.#M&quot;,<br>        &quot;formatType&quot;: &quot;pbiFormat&quot;,<br>        &quot;labelPadding&quot;: 18    // &lt;-- 18px padding between y-axis and chart<br>      }<br>    }<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uyiTBaHHCRwm_XzH9h27Sw.png" /></figure><p>Champion. Chart look fab, axes are on fleek. You’ll be pleased to know that was the hardest part… we will cruise through the next section, which is formatting the area mark properties.</p><p><strong>Step 6: Format Area Mark Properties</strong><br>There’s one mismatch between our PBI native chart and our Deneb area chart. Have you spotted it yet? Yup… correct… the bold pink outline (stroke) should be replaced. Let’s tackle that now:</p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;mark&quot;: {<br>    &quot;type&quot;: &quot;area&quot;,<br>    &quot;color&quot;: &quot;yellow&quot;,<br>    &quot;fill&quot;: &quot;#F0E199&quot;,<br>    &quot;stroke&quot;: &quot;#E044A7&quot;,   // &lt;-- delete the stroke attribute<br>    &quot;strokeWidth&quot;: 2       // &lt;-- and delete the strokeWidth<br><br>  },<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {...},<br>    &quot;y&quot;: {...}<br>  }<br>}</pre><p>And in big go, we make all the changes we need:</p><pre>{<br>  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},<br>  &quot;mark&quot;: {<br>    &quot;type&quot;: &quot;area&quot;,<br>    &quot;fill&quot;: &quot;#F0E199&quot;,   // &lt;-- change the fill colour<br>    &quot;opacity&quot;: 0.3,      // &lt;-- set the area colour opacity [0,1]<br>    &quot;line&quot;: {            // &lt;-- adjust line properties within area mark<br>      &quot;stroke&quot;: &quot;#E8D166&quot;,  // &lt;-- line stroke colour<br>      &quot;strokeWidth&quot;: 3      // &lt;-- line stroke width<br>    },<br>    &quot;point&quot;: {           // &lt;-- add points (aka markers in PBI)<br>      &quot;filled&quot;: true,    // &lt;-- markers are filled<br>      &quot;fill&quot;: &quot;#E044A7&quot;, // &lt;-- marker fill colour<br>      &quot;stroke&quot;: &quot;black&quot;, // &lt;-- black outline around markers<br>      &quot;strokeWidth&quot;: 0.2,  // &lt;-- marker outline stroke width<br>      &quot;size&quot;: 75         // &lt;-- marker size<br>    }<br>  },<br>  &quot;encoding&quot;: {<br>    &quot;x&quot;: {<br>      &quot;field&quot;: &quot;EndOfMonth&quot;,<br>      &quot;type&quot;: &quot;temporal&quot;,<br>      &quot;timeUnit&quot;: &quot;yearmonth&quot;,<br>      &quot;axis&quot;: {<br>        &quot;format&quot;: &quot;%b-%y&quot;,<br>        &quot;labelPadding&quot;: 8<br>      }<br>    },<br>    &quot;y&quot;: {<br>      &quot;field&quot;: &quot;_AC&quot;,<br>      &quot;type&quot;: &quot;quantitative&quot;,<br>      &quot;axis&quot;: {<br>        &quot;format&quot;: &quot;£0,,.#M&quot;,<br>        &quot;formatType&quot;: &quot;pbiFormat&quot;,<br>        &quot;labelPadding&quot;: 18<br>      }<br>    }<br>  }<br>}</pre><p>and can you believe it…!? Outstanding!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2m2m0I4zyfeFcRBQyS727g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*82jb22vBLeB5xVsAXnK7fQ.gif" /></figure><p>I hope you enjoyed this episode! Thanks for staying for the ride… until next time! 🧙‍♂️✨</p><p>🔗<strong>Github link to PBIX: </strong><a href="https://github.com/PBIQueryous/Deneb/blob/main/Medium-VegaLite-Series/EP04%20Deneb%20VegaLite%20Series%20-%20Styling%20Mark%20Properties%20pt2.pbix"><strong>EP04 Styling Mark Properties (pt2).pbix</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*ckWa6NTot3AfhfMwO7HQuw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6cafb8a2e552" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>