<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="318166" data-post-id="318166">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="drikanius" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/drikanius/120/33205_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  drikanius
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Abusing the topic a little (if you prefer, I’ll open a new one).</p>
<p>Is there a way to move this expr to an individual module and use it like this?</p>
<p>I have this calculate</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">    calculate :sales_total_amount, :integer do
      calculation expr(
                    sum(:transactions,
                      field: :sell_price,
                      query: [
                        filter: [
                          expr(
                            fragment(
                              "st_dwithin(?, ?::geography, ?)",
                              property_geography,
                              ^arg(:point),
                              ^arg(:distance_in_meters)
                            ) and
                              fragment(
                                "(? between ? and ?)",
                                buy_date,
                                ^arg(:start_date),
                                ^arg(:end_date)
                              ) and
                              not is_nil(sell_price)
                          )
                        ]
                      ]
                    )
                  )

      argument :start_date, :date, allow_nil?: false
      argument :end_date, :date, allow_nil?: false

      argument :distance_in_meters, :float, allow_nil?: false
      argument :point, :geo_any, allow_nil?: false
    end
</code></pre>
<p>I would like to have something like</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">    calculate :sales_total_amount, :integer do
      calculation CalculateHere

      argument :start_date, :date, allow_nil?: false
      argument :end_date, :date, allow_nil?: false

      argument :distance_in_meters, :float, allow_nil?: false
      argument :point, :geo_any, allow_nil?: false
    end
</code></pre>
<p>and a module CalculateHere similar with Ash.Calculation like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule CalculateHere do
  @moduledoc false

  use Ash.Calculation

  import Ash.Query

# it does not work, how to do it?
  def select(query, _opts, _context) do
    start_date = get_argument(query, :start_date)
    end_date = Ash.Query.get_argument(query, :end_date)

    query
    |&gt; Ash.Query.filter(
      expr(
        count(:transactions,
          query: [
            filter: [
              # expr(buy_date &gt; ^arg(:start_date) and buy_date &lt; ^arg(:end_date))
              expr(
                fragment(
                  "(? between ? and ?)",
                  buy_date,
                  ^start_date,
                  ^end_date
                ) and ...
              )
            ]
          ]
        )
      )
    )
  end

end
</code></pre>
<p>If you have an example or documentation talking about it, I would really appreciate it.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="318166" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/use-ash-keyset-pagination-with-ecto-queries/61690/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-318166" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="318166"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="318184" data-post-id="318184">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="zachdaniel" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/120/31980_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  zachdaniel
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Ash</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You’re looking for this <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule YourCalculation do
  use Ash.Calculation
  import Ash.Expr
  import Ash.Filter.TemplateHelpers

  def expression(_opts, _context) do
    expr(
      sum(:transactions,
        field: :sell_price,
        ...
        )
    )
  end
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="318184" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/use-ash-keyset-pagination-with-ecto-queries/61690/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-318184" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="318184"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="318566" data-post-id="318566">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="drikanius" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/drikanius/120/33205_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  drikanius
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi Zach!</p>
<p>Based on the calculations created in this topic, like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">    calculate :purchases_total_amount, :integer do
      calculation expr(
                    sum(:transactions,
                      field: :buy_price,
                      query: [
                        filter: [
                          expr(
                            fragment(
                              "st_dwithin(?, ?::geography, ?)",
                              property_geography,
                              ^arg(:point),
                              ^arg(:distance_in_meters)
                            ) and
                              fragment(
                                "(? between ? and ?)",
                                buy_date,
                                ^arg(:start_date),
                                ^arg(:end_date)
                              )
                          )
                        ]
                      ]
                    )
                  )
</code></pre>
<p>Load is working great, just like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Ash.Query.load(
      purchases_total_amount: %{
        start_date: start_date,
        end_date: end_date,
        point: geo_point,
        distance_in_meters: distance
      }
    )
</code></pre>
<p>Is there a way to sort (desc/asc) by this calculation? I tried to just use Ash.Query.sort(purchases_toltal_amount: : desc) but got this error:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Invalid value provided for calculation argument start_date in purchases_total_amount: : is required.

nil

    at sort
</code></pre>
<p>How to send argument to a load function?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="318566" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/use-ash-keyset-pagination-with-ecto-queries/61690/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-318566" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="318566"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="318568" data-post-id="318568">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="drikanius" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/drikanius/120/33205_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  drikanius
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Found it</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="318568" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/use-ash-keyset-pagination-with-ecto-queries/61690/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-318568" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="318568"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>