<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic GAMMA in Quick Measures Gallery</title>
    <link>https://community.fabric.microsoft.com/t5/Quick-Measures-Gallery/GAMMA/m-p/1081907#M510</link>
    <description>&lt;P&gt;&lt;A title="" href="https://community.powerbi.com/t5/Community-Blog/Excel-to-DAX-Translation/ba-p/1060991" target="_self"&gt;In my recent quest to create or catalog as many DAX equivalents for Excel functions&lt;/A&gt;&lt;SPAN&gt;, this is an approximation of the Gamma function using &lt;A href="https://en.wikipedia.org/wiki/Lanczos_approximation" target="_self"&gt;Lanczos' approximation&lt;/A&gt;.&amp;nbsp; Had to learn Python to get it coded in DAX since the Wikipedia page had the example code in Python. So...I learned that today.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;GAMMA = 
    VAR __zInput = MAX('Table'[z])
    VAR __p =
        {
            (0, 676.5203681218851),
            (1, -1259.1392167224028),
            (2, 771.32342877765313),
            (3, -176.61502916214059),
            (4, 12.507343278686905),
            (5, -0.13857109526572012),
            (6, 9.9843695780195716e-6),
            (7, 1.5056327351493116e-7)
        }
    VAR __EPSILON = 1e-7
    VAR __z = IF(__zInput &amp;lt; 0.5, 1 - __zInput - 1,__zInput - 1)
    VAR __pTable = 
        ADDCOLUMNS(
            __p,
            "x",[Value2] / (__z + [Value1] + 1)
        )
    VAR __x = 0.99999999999980993 + SUMX(__pTable,[x])
    VAR __t = __z + COUNTROWS(__pTable) - .5
    VAR __y = 
        IF(
            __zInput &amp;lt; 0.5,
            PI() / (SIN(PI() * __zInput) * SQRT(2*PI()) * POWER(__t,__z+0.5) * EXP(-1*__t) * __x),
            SQRT(2*PI()) * POWER(__t,__z+0.5) * EXP(-1*__t) * __x
        )
RETURN
    __y&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the cool part of this is that the original Python code had a for loop in it and a recursive element to it but I was able to code it in DAX even though DAX has no looping or recursion. So that was pretty nifty.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I had originally used&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;A href="https://en.wikipedia.org/wiki/Stirling%27s_approximation" target="_self"&gt;Stirling's approximation&lt;/A&gt;. That is GAMMA1. I wouldn't use that one though, it is not nearly as accurate especially for low values of z &amp;lt; .5. But, here it is anyway.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;GAMMA1 = 
	VAR __z = MAX('Table'[z])
RETURN
	EXP(
		.5 * 
		(LN
			( 2 * PI() ) - LN(__z)
		) + __z * 
		(
			LN(
				__z + 1 / ( 12 * __z - 1 / 10 * __z) 
			) 
			- 1 
		)
	)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We can use our new GAMMA function to also calculate GAMMALN, GAMMALN.PRECISE and GAMMA.DIST&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;GAMMALN = LN([GAMMA])

GAMMA.DIST = 
    VAR __x = [x]
    VAR __Alpha = [Alpha]
    VAR __Beta = [Beta]
    VAR __GAMMA = 
        VAR __zInput = __Alpha
        VAR __p =
            {
                (0, 676.5203681218851),
                (1, -1259.1392167224028),
                (2, 771.32342877765313),
                (3, -176.61502916214059),
                (4, 12.507343278686905),
                (5, -0.13857109526572012),
                (6, 9.9843695780195716e-6),
                (7, 1.5056327351493116e-7)
            }
        VAR __EPSILON = 1e-7
        VAR __z = IF(__zInput &amp;lt; 0.5, 1 - __zInput - 1,__zInput - 1)
        VAR __pTable = 
            ADDCOLUMNS(
                __p,
                "x",[Value2] / (__z + [Value1] + 1)
            )
        VAR __x = 0.99999999999980993 + SUMX(__pTable,[x])
        VAR __t = __z + COUNTROWS(__pTable) - .5
        VAR __y = 
            IF(
                __zInput &amp;lt; 0.5,
                PI() / (SIN(PI() * __zInput) * SQRT(2*PI()) * POWER(__t,__z+0.5) * EXP(-1*__t) * __x),
                SQRT(2*PI()) * POWER(__t,__z+0.5) * EXP(-1*__t) * __x
            )
    RETURN
        __y
RETURN
    DIVIDE(
        POWER(__x,__Alpha - 1) * EXP(-1*__x/__Beta),
        POWER(__Beta,__Alpha) * __GAMMA
    )&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have not yet been able to solve the cumulative form of the GAMMA.DIST function or the GAMMA.DIST.INV&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="reportid hidden"&gt;eyJrIjoiYzUyNGUxNjQtYmIyOS00ZWFhLTlkODEtNDIyZGNmMGUyYTViIiwidCI6IjRhMDQyNzQzLTM3M2EtNDNkMi04MjdiLTAwM2Y0YzdiYTFlNSIsImMiOjN9&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Sat, 09 May 2020 17:26:45 GMT</pubDate>
    <dc:creator>Greg_Deckler</dc:creator>
    <dc:date>2020-05-09T17:26:45Z</dc:date>
    <item>
      <title>GAMMA</title>
      <link>https://community.fabric.microsoft.com/t5/Quick-Measures-Gallery/GAMMA/m-p/1081907#M510</link>
      <description>&lt;P&gt;&lt;A title="" href="https://community.powerbi.com/t5/Community-Blog/Excel-to-DAX-Translation/ba-p/1060991" target="_self"&gt;In my recent quest to create or catalog as many DAX equivalents for Excel functions&lt;/A&gt;&lt;SPAN&gt;, this is an approximation of the Gamma function using &lt;A href="https://en.wikipedia.org/wiki/Lanczos_approximation" target="_self"&gt;Lanczos' approximation&lt;/A&gt;.&amp;nbsp; Had to learn Python to get it coded in DAX since the Wikipedia page had the example code in Python. So...I learned that today.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;GAMMA = 
    VAR __zInput = MAX('Table'[z])
    VAR __p =
        {
            (0, 676.5203681218851),
            (1, -1259.1392167224028),
            (2, 771.32342877765313),
            (3, -176.61502916214059),
            (4, 12.507343278686905),
            (5, -0.13857109526572012),
            (6, 9.9843695780195716e-6),
            (7, 1.5056327351493116e-7)
        }
    VAR __EPSILON = 1e-7
    VAR __z = IF(__zInput &amp;lt; 0.5, 1 - __zInput - 1,__zInput - 1)
    VAR __pTable = 
        ADDCOLUMNS(
            __p,
            "x",[Value2] / (__z + [Value1] + 1)
        )
    VAR __x = 0.99999999999980993 + SUMX(__pTable,[x])
    VAR __t = __z + COUNTROWS(__pTable) - .5
    VAR __y = 
        IF(
            __zInput &amp;lt; 0.5,
            PI() / (SIN(PI() * __zInput) * SQRT(2*PI()) * POWER(__t,__z+0.5) * EXP(-1*__t) * __x),
            SQRT(2*PI()) * POWER(__t,__z+0.5) * EXP(-1*__t) * __x
        )
RETURN
    __y&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the cool part of this is that the original Python code had a for loop in it and a recursive element to it but I was able to code it in DAX even though DAX has no looping or recursion. So that was pretty nifty.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I had originally used&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;A href="https://en.wikipedia.org/wiki/Stirling%27s_approximation" target="_self"&gt;Stirling's approximation&lt;/A&gt;. That is GAMMA1. I wouldn't use that one though, it is not nearly as accurate especially for low values of z &amp;lt; .5. But, here it is anyway.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;GAMMA1 = 
	VAR __z = MAX('Table'[z])
RETURN
	EXP(
		.5 * 
		(LN
			( 2 * PI() ) - LN(__z)
		) + __z * 
		(
			LN(
				__z + 1 / ( 12 * __z - 1 / 10 * __z) 
			) 
			- 1 
		)
	)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We can use our new GAMMA function to also calculate GAMMALN, GAMMALN.PRECISE and GAMMA.DIST&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;GAMMALN = LN([GAMMA])

GAMMA.DIST = 
    VAR __x = [x]
    VAR __Alpha = [Alpha]
    VAR __Beta = [Beta]
    VAR __GAMMA = 
        VAR __zInput = __Alpha
        VAR __p =
            {
                (0, 676.5203681218851),
                (1, -1259.1392167224028),
                (2, 771.32342877765313),
                (3, -176.61502916214059),
                (4, 12.507343278686905),
                (5, -0.13857109526572012),
                (6, 9.9843695780195716e-6),
                (7, 1.5056327351493116e-7)
            }
        VAR __EPSILON = 1e-7
        VAR __z = IF(__zInput &amp;lt; 0.5, 1 - __zInput - 1,__zInput - 1)
        VAR __pTable = 
            ADDCOLUMNS(
                __p,
                "x",[Value2] / (__z + [Value1] + 1)
            )
        VAR __x = 0.99999999999980993 + SUMX(__pTable,[x])
        VAR __t = __z + COUNTROWS(__pTable) - .5
        VAR __y = 
            IF(
                __zInput &amp;lt; 0.5,
                PI() / (SIN(PI() * __zInput) * SQRT(2*PI()) * POWER(__t,__z+0.5) * EXP(-1*__t) * __x),
                SQRT(2*PI()) * POWER(__t,__z+0.5) * EXP(-1*__t) * __x
            )
    RETURN
        __y
RETURN
    DIVIDE(
        POWER(__x,__Alpha - 1) * EXP(-1*__x/__Beta),
        POWER(__Beta,__Alpha) * __GAMMA
    )&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have not yet been able to solve the cumulative form of the GAMMA.DIST function or the GAMMA.DIST.INV&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="reportid hidden"&gt;eyJrIjoiYzUyNGUxNjQtYmIyOS00ZWFhLTlkODEtNDIyZGNmMGUyYTViIiwidCI6IjRhMDQyNzQzLTM3M2EtNDNkMi04MjdiLTAwM2Y0YzdiYTFlNSIsImMiOjN9&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 09 May 2020 17:26:45 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Quick-Measures-Gallery/GAMMA/m-p/1081907#M510</guid>
      <dc:creator>Greg_Deckler</dc:creator>
      <dc:date>2020-05-09T17:26:45Z</dc:date>
    </item>
  </channel>
</rss>

