Phoenix Ecto conditional where

Possibly like this. Indeed, as per @fuelen, use of dynamic/2 is the key.

import Ecto.Query

def search(term) when is_binary(term)
  CashFlowEntry |> search_where(term) |> Repo.all()
end

defp search_where(query, term) when is_binary(term) do
  case Decimal.parse(term) do
    {amount, _} -> where(query, [x], x.amount == ^amount or ilike(x.note, ^term))
    _ -> where(query, [x], ilike(x.note, ^term))
  end
end